Question #42   Submitted by Answiki on 06/06/2020 at 11:52:52 AM UTC

How to iterate through JavaScript arrays?

Answer   Submitted by Answiki on 09/02/2022 at 08:02:49 PM UTC

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
})


ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}

6 events in history
Answer by Answiki on 09/02/2022 at 08:02:49 PM

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
})


ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}

Answer by Answiki on 10/11/2020 at 08:20:28 AM

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
})


ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}

Answer by Answiki on 06/06/2020 at 12:18:21 PM

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
})

ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}

Answer by Answiki on 06/06/2020 at 12:17:33 PM

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
})

ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}




Answer by Answiki on 06/06/2020 at 12:16:42 PM

To iterate through JS arrays, you have 3 options :

  • Sequential for loop
  • Array.prototype.forEach
  • ES6 for-of statement


Sequential for loop

var myArray = ['First', 'Second', 'Third'];
for (var i = 0; i < myArray.length; i++) {
	// Do awesome stuff here
    console.log(myArray[i]);
}


Array.prototype.forEach

var myArray = ['First', 'Second', 'Third'];
myArray.forEach(function (value, index) {
	// Do awesome stuff here
    console.log(index, value);
}

ES6 for-of statement

var myArray = ['First', 'Second', 'Third'];
for (const value of myArray){
	// Do awesome stuff here
    console.log(value);
}




Question by Answiki 06/06/2020 at 11:52:52 AM
How to iterate through JavaScript arrays?
# ID Query URL Count

Icons proudly provided by Friconix.