Array Methods
Introduction to array methods that use functions.
In this lesson, we’ll discuss array methods and their convenient usages.
Introduction
Methods of an object are property names assigned to functions. These methods can be invoked to execute the function. Now, let’s take a look at different methods.
forEach
method
forEach
method is used to iterate an array in loops. Let’s dig deeper into this method.
Syntax of forEach
method
The syntax of the forEach
method is as follows.
arr.forEach(<function>);
The forEach
method above takes, as an argument, a function invoked for each element in the array. The function to be passed is given arguments in the following order, by the forEach
method.
function callbackfn(value: any, index: number, array: any[])
For each element, the callbackfn
is passed with the value of the element as the first argument, followed by the index of the element as the second argument, and lastly the array itself as the third argument. We may write the callbackfn
function to take 0 to 3 arguments.
Look at a couple of examples.
Get hands-on with 1400+ tech skills courses.