Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JavaScript»How to Loop Over an Array in JavaScript

    How to Loop Over an Array in JavaScript

    RahulBy RahulNovember 8, 20151 Min Read

    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 Loop

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

    JavaScript
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <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 forEach

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

    JavaScript
    1
    2
    3
    4
    5
    6
    7
    8
    <script type="text/javascript">
     
       var arr = ["Jan", "Feb", "Mar", "Apr"];
       arr.forEach(function(entry) {
          console.log(entry);
       });
     
    </script>

    javascript js
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Remove Array Element by Value in JavaScript
    Next Article How to Check if Checkbox is Checked with JavaScript

    Related Posts

    JavaScript Program to Add Two Numbers

    2 Mins Read

    How to Install ReactJS on Ubuntu 20.04

    3 Mins Read

    Remove First Character from String in JavaScript

    1 Min Read

    10 JavaScript Console Methods

    Updated:November 14, 20203 Mins Read

    JavaScript Program to Remove Duplicate Array Elements

    Updated:July 20, 20211 Min Read

    How to Use JSON.parse() and JSON.stringify()

    2 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    • (Resolved) Please install all available updates for your release before upgrading
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.