For-each syntax in JavaScript

Asked 2 years ago, Updated 2 years ago, 95 views

In Java, there's the same for syntax at the bottom.

String[] myStringArray = {"Hello","World"};
for(String s : myStringArray)
{
    //Do something
}

Is this not available in JavaScript?

array for-loop javascript

2022-09-22 22:09

1 Answers

Normal

for(var i=0; i<array.length; i++) {
    console.log(array[i]);
}

Like this

for(var i in array) {
    console.log(array[i]);
}

Although there is this forin format, it does not work properly with IE8, so we recommend that you use the above method.


2022-09-22 22:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.