Home > Article > Web Front-end > How to use forEach method
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.
Let’s first take a look at the basic syntax of forEach()
array.forEach(function(currentValue, index, arr), thisValue)
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>
The execution results are as follows
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!