WebMasterCampus
WEB DEVELOPER Resources

Javascript for Each Loop an Array

Learn how to use Javascript for Each Loop an Array


The forEach() method uses callback function once for each element in an array in ascending order.

forEach Loop Syntax


array.forEach(callback(currentValue [, index [, array]])[, thisArg])

Examples Of forEach()


  <script>
    a = [1,2,3];

    a.forEach(function(value){
      console.log(value);
    });


    var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"];

    month.forEach(function(value){
         console.log(value);
    });

    var week = new Array( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" );

    week.forEach(processWeek);

    function processWeek(value){          
          console.log(value);
    }
  </script>

Reference

tc39.es

Created with love and passion.