5

How to loop through an array in JavaScript

 1 year ago
source link: https://www.laravelcode.com/post/how-to-loop-through-an-array-in-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to loop through an array in JavaScript

  1261 views

  2 years ago

jQuery

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>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK