ES6 - Array Method forEach()


Advertisements

forEach() method calls a function for each element in the array.

Syntax

array.forEach(callback[, thisObject]);  

Parameters

  • callback − Function to test for each element.

  • thisObject − Object to use as this when executing callback.

Return Value

Returns created array.

Example: forEach()

var nums = new Array(12,13,14,15)  
console.log("Printing original array......")  

nums.forEach(function(val,index) { 
   console.log(val) 
})  
nums.reverse()  //reverses the array element 
console.log("Printing Reversed array....")  

nums.forEach(function(val,index){ 
   console.log(val) 
})  

Output

Printing Original Array…. 
12 
13 
14 
15 
Printing Reversed array… 
15 
14 
13 
1
es6_arrays.htm
Advertisements