Skip to content

Instantly share code, notes, and snippets.

@imparvez
Created September 22, 2017 09:34
Show Gist options
  • Save imparvez/4803442610a1cc919811aede968df92c to your computer and use it in GitHub Desktop.
Save imparvez/4803442610a1cc919811aede968df92c to your computer and use it in GitHub Desktop.
// Different ways to loop or iterate arrays in JavaScript
var newArray = ['item 1', 'item 2', 'item 3', 'item 4'];
// Way no: 1
for(var i = 0; i < newArray.length; i++) {
console.log('Array Item', newArray[i]);
}
// Way no: 2
for(var i in newArray) {
console.log('Array Item: ', newArray[i]);
}
// Way no: 3
newArray.forEach(function(value){
console.log('Array Items: ', value);
})
// Way no: 4
newArray.map(function(value, index){
console.log('Array Items => ', value);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment