Web Technologies - JAVASCRIPT

Back to Course

Lesson Description

Lession - #220 Javascript Array Iteration

Array iteraration method work on each array item.


is used is used to iterate items in javascript array.

The JavaScript forEach loop is an Array method that executes a custom callback function on every item in a array. The forEach loop must be utilized on Arrays, Sets, and Maps. A forEach loop will run a JavaScript callback function for every item in a list. Then, at that point, the loop stops.

Let's see an example

      <title>JavaScript Array forEach Method</title>
      <script type = "text/javascript">
         if (!Array.prototype.forEach>
{ Array.prototype.forEach = function(fun /*, thisp*/>
{ var len = this.length; if (typeof fun != "function">
throw new TypeError(>
; var thisp = arguments[1]; for (var i = 0; i < len; i++>
{ if (i in this>
fun.call(thisp, this[i], i, this>
; } }; } function printBr(element, index, array>
{ document.write("
[" + index + "] is " + element >
; } [12, 5, 8, 130, 44].forEach(printBr>
; </script> </body> </html>


Try it here