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++){

// 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.


Popular posts from this blog

Open-graph meta data tags introdution

What is 'this.' keyword in JavaScript

How to create zip archives in nodejs

what is 'array.includes' function in JavaScript

How to build a twitter retweet bot using JavaScript