foreach is not an es6 method. foreach is a method for traversing arrays in es3. It can call each element of the array and pass the elements to the callback function for processing. The syntax is "array.forEach(function(current element, index, array){...})" ;This method does not handle empty arrays.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 3, Dell G3 computer.
foreach is not an es6 method.
foreach is a method for traversing arrays in es3.
The forEach() method is used to call each element of the array and pass the element to the callback function for processing.
array.forEach(function(currentValue, index, arr){...})
currentValue Required. Current element
#index Optional. The index value of the current element.
arr Optional. The array object to which the current element belongs.
Return value: undefined
Example 1: Calculate the sum of all elements of the array
var sum = 0; var numbers = [65, 44, 12, 4]; numbers.forEach(function(item) { sum += item; console.log(sum); });
Example 2: Multiply all values in the array by the number 3
var sum = 0; var numbers = [65, 44, 12, 4]; numbers.forEach(function(item,index,arr) { item = item * 3; console.log(item); });
[Related recommendations: javascript video tutorial, web front-end 】
The above is the detailed content of Is foreach in es6?. For more information, please follow other related articles on the PHP Chinese website!