SHARE
TWEET

Untitled

a guest Sep 22nd, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Different ways to loop or iterate arrays in JavaScript
  2. var newArray = ['item 1', 'item 2', 'item 3', 'item 4'];
  3.  
  4. // Way no: 1
  5. for(var i = 0; i < newArray.length; i++) {
  6.   console.log('Array Item', newArray[i]);
  7. }
  8.  
  9. // Way no: 2
  10. for(var i in newArray) {
  11.   console.log('Array Item: ', newArray[i]);
  12. }
  13.  
  14. // Way no: 3
  15. newArray.forEach(function(value){
  16.   console.log('Array Items: ', value);
  17. })
  18.  
  19. // Way no: 4
  20. newArray.map(function(value, index){
  21.   console.log('Array Items => ', value);
  22. })
RAW Paste Data
Top