Skip to main content
added correct reference
Source Link

6 different methods to loop through the array

You can loop through an array by many different methods. I have sorted my 6 favorite methods from top to bottom.

1. Using JavaScript for loopJavaScript for loop

When it's to simply loop through an array, the for loop is my first choice.

let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

2. Using forEach loop

forEach loop is a modern way to loop through the array. Also, it gives more flexibility and control over the array and elements.

let array = [1, 2, 3, 4, 5];
array.forEach((element) => {
  console.log(element);
});

3. Using for...of

for...of loop gives you direct access to the array elements.

let array = [1, 2, 3, 4, 5];
for (let element of array) {
  console.log(element);
}

4. Using for...in loop

for...in gives you a key using which you can access array elements.

let array = [1, 2, 3, 4, 5];
for(let index in array){
  console.log(array[index]);
}

5. Using while loop

while loop is can be used to loop through the array as well.

let array = [1, 2, 3, 4, 5];
let length = array.length;
while(length > 0){
  console.log(array[array.length - length]);
  length--;
}

6. Using do...while loop

Likewise, I use do...while loop

let array = [1, 2, 3, 4, 5];
let length = array.length;
do {
  console.log(array[array.length - length]);
  length--;
}
while (length > 0)

6 different methods to loop through the array

You can loop through an array by many different methods. I have sorted my 6 favorite methods from top to bottom.

1. Using JavaScript for loop

When it's to simply loop through an array, the for loop is my first choice.

let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

2. Using forEach loop

forEach loop is a modern way to loop through the array. Also, it gives more flexibility and control over the array and elements.

let array = [1, 2, 3, 4, 5];
array.forEach((element) => {
  console.log(element);
});

3. Using for...of

for...of loop gives you direct access to the array elements.

let array = [1, 2, 3, 4, 5];
for (let element of array) {
  console.log(element);
}

4. Using for...in loop

for...in gives you a key using which you can access array elements.

let array = [1, 2, 3, 4, 5];
for(let index in array){
  console.log(array[index]);
}

5. Using while loop

while loop is can be used to loop through the array as well.

let array = [1, 2, 3, 4, 5];
let length = array.length;
while(length > 0){
  console.log(array[array.length - length]);
  length--;
}

6. Using do...while loop

Likewise, I use do...while loop

let array = [1, 2, 3, 4, 5];
let length = array.length;
do {
  console.log(array[array.length - length]);
  length--;
}
while (length > 0)

6 different methods to loop through the array

You can loop through an array by many different methods. I have sorted my 6 favorite methods from top to bottom.

1. Using JavaScript for loop

When it's to simply loop through an array, the for loop is my first choice.

let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

2. Using forEach loop

forEach loop is a modern way to loop through the array. Also, it gives more flexibility and control over the array and elements.

let array = [1, 2, 3, 4, 5];
array.forEach((element) => {
  console.log(element);
});

3. Using for...of

for...of loop gives you direct access to the array elements.

let array = [1, 2, 3, 4, 5];
for (let element of array) {
  console.log(element);
}

4. Using for...in loop

for...in gives you a key using which you can access array elements.

let array = [1, 2, 3, 4, 5];
for(let index in array){
  console.log(array[index]);
}

5. Using while loop

while loop is can be used to loop through the array as well.

let array = [1, 2, 3, 4, 5];
let length = array.length;
while(length > 0){
  console.log(array[array.length - length]);
  length--;
}

6. Using do...while loop

Likewise, I use do...while loop

let array = [1, 2, 3, 4, 5];
let length = array.length;
do {
  console.log(array[array.length - length]);
  length--;
}
while (length > 0)

Added support link
Source Link

6 different methods to loop through the array

You can loop through an array by many different methods. I have sorted my 6 favorite methods from top to bottom.

1. Using for loopJavaScript for loop

When it's to simply loop through an array, the for loop is my first choice.

let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

2. Using forEach loop

forEach loop is a modern way to loop through the array. Also, it gives more flexibility and control over the array and elements.

let array = [1, 2, 3, 4, 5];
array.forEach((element) => {
  console.log(element);
});

3. Using for...of

for...of loop gives you direct access to the array elements.

let array = [1, 2, 3, 4, 5];
for (let element of array) {
  console.log(element);
}

4. Using for...in loop

for...in gives you a key using which you can access array elements.

let array = [1, 2, 3, 4, 5];
for(let index in array){
  console.log(array[index]);
}

5. Using while loop

while loop is can be used to loop through the array as well.

let array = [1, 2, 3, 4, 5];
let length = array.length;
while(length > 0){
  console.log(array[array.length - length]);
  length--;
}

6. Using do...while loop

Likewise, I use do...while loop

let array = [1, 2, 3, 4, 5];
let length = array.length;
do {
  console.log(array[array.length - length]);
  length--;
}
while (length > 0)

6 different methods to loop through the array

You can loop through an array by many different methods. I have sorted my 6 favorite methods from top to bottom.

1. Using for loop

When it's to simply loop through an array, the for loop is my first choice.

let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

2. Using forEach loop

forEach loop is a modern way to loop through the array. Also, it gives more flexibility and control over the array and elements.

let array = [1, 2, 3, 4, 5];
array.forEach((element) => {
  console.log(element);
});

3. Using for...of

for...of loop gives you direct access to the array elements.

let array = [1, 2, 3, 4, 5];
for (let element of array) {
  console.log(element);
}

4. Using for...in loop

for...in gives you a key using which you can access array elements.

let array = [1, 2, 3, 4, 5];
for(let index in array){
  console.log(array[index]);
}

5. Using while loop

while loop is can be used to loop through the array as well.

let array = [1, 2, 3, 4, 5];
let length = array.length;
while(length > 0){
  console.log(array[array.length - length]);
  length--;
}

6. Using do...while loop

Likewise, I use do...while loop

let array = [1, 2, 3, 4, 5];
let length = array.length;
do {
  console.log(array[array.length - length]);
  length--;
}
while (length > 0)

6 different methods to loop through the array

You can loop through an array by many different methods. I have sorted my 6 favorite methods from top to bottom.

1. Using JavaScript for loop

When it's to simply loop through an array, the for loop is my first choice.

let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

2. Using forEach loop

forEach loop is a modern way to loop through the array. Also, it gives more flexibility and control over the array and elements.

let array = [1, 2, 3, 4, 5];
array.forEach((element) => {
  console.log(element);
});

3. Using for...of

for...of loop gives you direct access to the array elements.

let array = [1, 2, 3, 4, 5];
for (let element of array) {
  console.log(element);
}

4. Using for...in loop

for...in gives you a key using which you can access array elements.

let array = [1, 2, 3, 4, 5];
for(let index in array){
  console.log(array[index]);
}

5. Using while loop

while loop is can be used to loop through the array as well.

let array = [1, 2, 3, 4, 5];
let length = array.length;
while(length > 0){
  console.log(array[array.length - length]);
  length--;
}

6. Using do...while loop

Likewise, I use do...while loop

let array = [1, 2, 3, 4, 5];
let length = array.length;
do {
  console.log(array[array.length - length]);
  length--;
}
while (length > 0)

Remove undisclosed self-promotion.
Source Link
Martijn Pieters
  • 1.1m
  • 313
  • 4.2k
  • 3.4k

6 different methods to loop through the array

You can loop through an array by many different methods. I have sorted my 6 favorite methods from top to bottom.

1. Using for loopfor loop

When it's to simply loop through an array, the for loop is my first choice.

let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

2. Using forEach loop

forEach loop is a modern way to loop through the array. Also, it gives more flexibility and control over the array and elements.

let array = [1, 2, 3, 4, 5];
array.forEach((element) => {
  console.log(element);
});

3. Using for...of

for...of loop gives you direct access to the array elements.

let array = [1, 2, 3, 4, 5];
for (let element of array) {
  console.log(element);
}

4. Using for...in loop

for...in gives you a key using which you can access array elements.

let array = [1, 2, 3, 4, 5];
for(let index in array){
  console.log(array[index]);
}

5. Using while loop

while loop while loop is can be used to loop through the array as well.

let array = [1, 2, 3, 4, 5];
let length = array.length;
while(length > 0){
  console.log(array[array.length - length]);
  length--;
}

6. Using do...while loop

Likewise, I use do...while loop

let array = [1, 2, 3, 4, 5];
let length = array.length;
do {
  console.log(array[array.length - length]);
  length--;
}
while (length > 0)

6 different methods to loop through the array

You can loop through an array by many different methods. I have sorted my 6 favorite methods from top to bottom.

1. Using for loop

When it's to simply loop through an array, the for loop is my first choice.

let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

2. Using forEach loop

forEach loop is a modern way to loop through the array. Also, it gives more flexibility and control over the array and elements.

let array = [1, 2, 3, 4, 5];
array.forEach((element) => {
  console.log(element);
});

3. Using for...of

for...of loop gives you direct access to the array elements.

let array = [1, 2, 3, 4, 5];
for (let element of array) {
  console.log(element);
}

4. Using for...in loop

for...in gives you a key using which you can access array elements.

let array = [1, 2, 3, 4, 5];
for(let index in array){
  console.log(array[index]);
}

5. Using while loop

while loop is can be used to loop through the array as well.

let array = [1, 2, 3, 4, 5];
let length = array.length;
while(length > 0){
  console.log(array[array.length - length]);
  length--;
}

6. Using do...while loop

Likewise, I use do...while loop

let array = [1, 2, 3, 4, 5];
let length = array.length;
do {
  console.log(array[array.length - length]);
  length--;
}
while (length > 0)

6 different methods to loop through the array

You can loop through an array by many different methods. I have sorted my 6 favorite methods from top to bottom.

1. Using for loop

When it's to simply loop through an array, the for loop is my first choice.

let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

2. Using forEach loop

forEach loop is a modern way to loop through the array. Also, it gives more flexibility and control over the array and elements.

let array = [1, 2, 3, 4, 5];
array.forEach((element) => {
  console.log(element);
});

3. Using for...of

for...of loop gives you direct access to the array elements.

let array = [1, 2, 3, 4, 5];
for (let element of array) {
  console.log(element);
}

4. Using for...in loop

for...in gives you a key using which you can access array elements.

let array = [1, 2, 3, 4, 5];
for(let index in array){
  console.log(array[index]);
}

5. Using while loop

while loop is can be used to loop through the array as well.

let array = [1, 2, 3, 4, 5];
let length = array.length;
while(length > 0){
  console.log(array[array.length - length]);
  length--;
}

6. Using do...while loop

Likewise, I use do...while loop

let array = [1, 2, 3, 4, 5];
let length = array.length;
do {
  console.log(array[array.length - length]);
  length--;
}
while (length > 0)

better explain
Source Link
Loading
Source Link
Loading