How to iterate array elements using 'for loop' in JavaScript
JavaScript arrays are similar to arrays in c and in c like programming languages such as php among others In JavaScript arrays are simply collections of elements for instance Objects Numbers or other Arrays
Declaring an array
An array may be declared using the array constructor for instance
const array = new Array()
or as
const array = []
Iterating an array using 'for' loop
This is the implest and most common method to iterate an array is by using the 'for loop' am sure you're familiar with the syntax. take for Instance you want to iterate an array of integers and print each one of them.
var ints = [1, 2, 3, 4]
To iterate ints using for loop.
for (var i = 0; i < ints.length; i++){
console.log(ints[i])
}
// output will be
// 1
// 2
// 3
// 4
In between the parentheses they are three procedures in three segments separated by colons
first segment declares var i an index which gets incriminated in the third segment as long as it's less than the length of the ints or as long as the statement in the second segment returns true.
Comments
Post a Comment