0 votes
1 view
in Web Technology by (26.9k points)

In Java you can use a for loop to traverse objects in an array as follows:

String[] myStringArray = {"Hello", "World"};

for (String s : myStringArray) { 

// Do something 

}

Can you do the same in JavaScript?

1 Answer

0 votes
by (65.9k points)

There are several ways to loop through an array in JavaScript one of them I am mentioning here:-

You can use the sequential for loop for this below is the code for the same:

var myStringArray = ["Hello","World"]; 

var arrayLength = myStringArray.length; 

for (var i = 0; i < arrayLength; i++) { console.log(myStringArray[i]); 

//Do something 

}

...