Q:

How to loop through an array in JavaScript

0

How to loop through an array in JavaScript

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the JavaScript for Loop

The easiest way to loop through or iterate over an array in JavaScript is using the for loop.

The following example will show you how to display all the values of an array in JavaScript one by one.

<script>
var fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];
    
// Loop through the fruits array and display all the values
for(var i = 0; i < fruits.length; i++){
    document.write("<p>" + fruits[i] + "</p>");
}
</script>

Alternatively, you can use the ES6 newly introduced for-of loop to iterate over an array, like this:

<script>
var fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];
    
// Loop through the fruits array and display all the values
for(var fruit of fruits){
    document.write("<p>" + fruit + "</p>");
}
</script>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to replace character inside a string in JavaSc... >>
<< How to select an element with multiple classes wit...