Breaking

Code Gripper
Responsive Advertisement
  • Home
Homejavascript

Loop through an array in JavaScript

byMahabubur Rahman -August 11, 2021
0

There are many way to loop JavaScript array, some of them are - 

  • for (var i = 0; i < xs.length; i++) { console.log(xs[i]); }
  • xs.forEach((x, i) => console.log(x));
  • for (const x of xs) { console.log(x); }
We explain in details bellow - 

Source: Stack Overflow 

Sequential for loop:

    var myStringArray = ["Hello","World"];
    var arrayLength = myStringArray.length;
    for (var i = 0; i < arrayLength; i++) {
        console.log(myStringArray[i]);
        //Do something
    }

Source: Stack Overflow 

forEach:

    const array = ["one", "two", "three"]
    array.forEach(function (item, index) {
      console.log(item, index);
    });

Source: Stack Overflow 

ES6 for of statement:

    let colors = ['red', 'green', 'blue'];
    for (const color of colors){
        console.log(color);
    }










Tags javascript
  • Facebook
  • Twitter

You may like these posts

Post a Comment

Previous Post Next Post

Follow Us

Popular Posts

Subscribe Us

Facebook

Categories

Main Tags

Code Gripper

September 30, 2023

Total Pageviews

Search This Blog

CODE GRIPPER

Code Gripper
CODE GRIPPER

  • Home
  • Python
  • PHP

Menu Footer Widget

  • Home
  • About
  • Contact Us
Code Gripper

About Us

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's.

Categories

Main Tags

Latest Posts

Most Popular

Design by Blogger | Distributed by Gooyaabi
  • Home
  • About Us
  • Contact Us
  • RTL Version

Contact Form