Array Helper Methods in ES6

Callback function

What is a Callback function? - MDN

: A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

A callback function (A) is a function passed as an argument to another function (B),

and is a function (A) that is executed inside the other function (B).

1. map()

Returns a new array containing the results of calling callbackFn on every element in this array.

ex)

['1', '2','3'].map(Number)

const numbers = [0, 9, 99]

function addOne(number) {
    return number + 1
}
const newNumbers1 = numbers.map(addOne)
console.log(newNumbers1)

const newNumbers2 = numbers.map(function(number) {
    // Iterates through [0, 9, 99], placing each element in the (number) position.
    // Then returns the value to a new array and returns it at the end.
    return number + 1
})
console.log(newNumbers2)

2. forEach()

Calls a callback function for each element in the array.

ex)

3. filter()

Returns the found element in the array, if some element in the array satisfies the testing callback function, or undefined if not found.

ex)

4. find()

Returns the found element in the array, if some element in the array satisfies the testing callback function, or undefined if not found.

ex)

5. every()

Returns true if every element in this array satisfies the testing callback function

ex)

Last updated