Is foreach in es6?

青灯夜游
Release: 2022-05-05 17:59:33
Original
2653 people have browsed it

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.

Is foreach in es6?

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.

Is foreach in es6?

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){...})
Copy after login
  • 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);
});
Copy after login

Is foreach in es6?

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);
});
Copy after login

Is foreach in es6?

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template