Array.forEach()

The forEach() method calls a function (a callback function) once for each array element.

Array iteration methods operate on every array item.

Note that the function takes 3 arguments:

  • The item value

  • The item index

  • The array itself

EXAMPLE ❯


Array.map()

The map() method creates a new array by performing a function on each array element.

The map() method does not execute the function for array elements without values.

The map() method does not change the original array.

Note that the function takes 3 arguments:

• The item value

• The item index

• The array itself

EXAMPLE ❯


Array.filter()

The filter() method creates a new array with array elements that passes a test.

This example creates a new array from elements with a value larger than 18:

const numbers = [45491625];
const over18 = numbers.filter(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}

In the example above, the callback function does not use the index and array parameters, so they can be omitted:

EXAMPLE ❯


Array.reduce()

The reduce() method runs a function on each array element to produce (reduce it to) a single value.

The reduce() method works from left-to-right in the array. See also reduceRight().

The reduce() method does not reduce the original array.

The reduce() method can accept an initial value:

const numbers = [45491625];
let sum = numbers.reduce(myFunction, 100);

function myFunction(total, value) {
  return total + value;
}

In this below example, It finds the sum of all numbers in an array:

EXAMPLE ❯


Array.reduceRight()

The reduceRight() method runs a function on each array element to produce (reduce it to) a single value.

The reduceRight() works from right-to-left in the array. See also reduce().

The reduceRight() method does not reduce the original array:

const numbers = [45491625];
let sum = numbers1.reduceRight(myFunction);

function myFunction(total, value, index, array) {
  return total + value;
}

Note that the function takes 4 arguments:

The total (the initial value / previously returned value)

The item value

The item index

The array itself

The example above does not use the index and array parameters.

It can be rewritten to below Example:

EXAMPLE ❯


Array.every()

The every() method check if all array values pass a test.

Note that the function takes 3 arguments:

• The item value

• The item index

• The array itself

EXAMPLE ❯


Array.some()

The some() method check if some array values pass a test.

Note that the function takes 3 arguments:

This example check if some array values are larger than 18:

EXAMPLE ❯


Array.indexOf()

The indexOf() method searches an array for an element value and returns its position.

Note: The first item has position 0, the second item has position 1, and so on.

Syntax:

array.indexOf(item, start)

Description:

item - Required. The item to search for.

Start - Optional. Where to start the search. Negative values will start at the given position

Array.indexOf() returns -1 if the item is not found.

If the item is present more than once, it returns the position of the first occurrence.

counting from the end, and search to the end.

Example to Search an array for the item "Apple":

EXAMPLE ❯


Array.includes()

This allows us to check if an element is present in an array (including NaN, unlike indexOf).

Syntax:

array.includes(search-item)

 

Array.includes() allows to check for NaN values. Unlike Array.indexOf().

EXAMPLE ❯


Array.find()

The find() method returns the value of the first array element that passes a test function.

This example finds (returns the value of) the first element that is larger than 18:

EXAMPLE ❯


Array.findIndex()

The findIndex() method returns the index of the first array element that passes a test function.

This example finds the index of the first element that is larger than 18:

EXAMPLE ❯


Array.from()

The Array.from() method returns an Array object from any object with a length property or any iterable object.

Example:

Create an Array from a String:

EXAMPLE ❯


Array.Keys()

The Array.keys() method returns an Array Iterator object with the keys of an array.

EXAMPLE ❯