How to Loop Over an Array in JavaScript
Abstract: var arr = ["index <
This tutorial will help you to initialize an array using JavaScript and navigate to array with one by one elements in a loop. Here you will find two methods for navigating through an array in JavaScript.
Method 1 – Using For LoopSometimes you may like to use older ways that is easy to use. This is simple c programming like for loop, easy to use and works perfect.
<script type="text/javascript"> var index; var arr = ["Jan", "Feb", "Mar", "Apr"]; for (index = 0; index < arr.length; ++index) { console.log(arr[index]); } </script>123456789<script type="text/javascript"> var index; var arr = ["Jan", "Feb", "Mar", "Apr"]; for (index = 0; index < arr.length; ++index) { console.log(arr[index]); } </script> Method 2 – Using forEachThe forEach function calls callbackfn function one time for each element present in the array, in ascending index order. This is also useful for navigating through an array.
JavaScript forEach() function is also useful for navigating though array elements.