Home > Web Front-end > JS Tutorial > How to use forEach method

How to use forEach method

不言
Release: 2019-02-15 10:05:31
Original
13522 people have browsed it

The forEach() method in How to use forEach method is used to call each element of the array and pass the element to the callback function. Let's take a closer look at how to use the forEach method.

How to use forEach method

Let’s first take a look at the basic syntax of forEach()

array.forEach(function(currentValue, index, arr), thisValue)
Copy after login

function represents a callback function, which is required.

currentValue represents the value of the current element processed in typedArray.

index represents the index of the current element being processed in the array.

arra represents the array in which the forEach() function is being called.

Let’s look at the specific example of the forEach() method

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
<script type="text/javascript">
var fruits=["apple", "banana", "orange", "strawberry"];
fruits.forEach(function(eat) {
    console.log(eat);
});
</script>
</body>
</html>
Copy after login

The execution results are as follows

How to use forEach method

This article ends here. For more exciting content, you can pay attention to other related column tutorials on the php Chinese website! !

The above is the detailed content of How to use forEach method. 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
Latest Issues
foreach problem
From 1970-01-01 08:00:00
0
0
0
Loop problem, foreach
From 1970-01-01 08:00:00
0
0
0
About the parameters of php foreach
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template