In Javascript, the forEach()
method is used to iterate over an array and execute a function for each element. Here is an example:
index.tsx95 chars5 lines
Output:
index.tsx10 chars6 lines
In this example, we first declare an array myArray
with 5 elements. We then call the forEach()
method on the array and pass in a function that takes an argument element
. The function simply logs the value of element
to the console. The forEach()
method will then execute this function for each element in the array.
You can also use an arrow function to make the code more concise:
index.tsx81 chars3 lines
Output:
index.tsx10 chars6 lines
gistlibby LogSnag