Looping through Arrays

Loops and Iteration

  • Loops offer a quick and easy way to do something repeatedly

Javascript offers many different ways to loop, click here for more information


Using .forEach() (native javascript)

  • Native javascript offers a .forEach() method loops through (iterates) an array
  let fruits=[“Banana”,”Apple”,”Pear”];

  fruits.forEach(function(element,index){
    console.log(element,index);
  });

JS Bin on jsbin.com

click here for more information about the forEach() loop


Using $.each() (jQuery)

  • jQuery also provides a way to iterate arrays through its $.each() method
  let fruits=[“Banana”,”Apple”,”Pear”];

  $.each(fruits, function(index, element){
    console.log(index, element);
  });

JS Bin on jsbin.com

click here for more information about the jQuery’s $.each() loop