You are on page 1of 12

JavaScript

Array Methods

Finding elements
High-order methods

Part-2
Finding elements

indexOf()
To find the position of an element in an
array, you use the indexOf() method.
This method returns the index of the
first occurrence the element that you
want to find, or -1 if the element is not
found.
Finding elements

includes()
The includes() method returns true if an
array contains a given element;
Otherwise, it returns false.
ECMAScript 2016 standardized this functionality by
providing the Array.prototype.includes() method.
Finding elements

find()

The find() method returns the first element


in an array.
The find() accepts two arguments: a
callback function and an optional value to
use for the this inside the callback function.
ES6 introduced a new method called find()
Finding elements

findIndex()

The findIndex() method returns the index of


the element that satisfies a testing function
or -1 if no element passed the test.
ES6 added a new method called findIndex()
High-order methods

map()

The map() method of Array instances


creates a new array populated with the
results of calling a provided function on
every element in the calling array.
High-order methods

filter()
The filter() method of array instances
creates a shallow copy of a portion of a
given array, filtered down to just the
elements from the given array that pass the
test implemented by the provided function.
High-order methods

reduce()
The reduce() method of array instances
executes a user-supplied "reducer" callback
function on each element of the array,in
order, passing in the return value from the
calculation on the preceding element. The
final result of running the reducer across
all elements of the array is a single value
High-order methods

every()
The every() method of Array instances tests
whether all elements in the array pass the
test implemented by the provided function.
It returns a Boolean value.
High-order methods

some()
The some() method of Array instances tests
whether at least one element in the array
passes the test implemented by the
provided function. It returns true if, in the
array, it finds an element for which the
provided function returns true; otherwise it
returns false. It doesn't modify the array.
High-order methods

sort()
The sort() method of Array instances sorts
the elements of an array in place and
returns the reference to the same array,
now sorted. The default sort order is
ascending, built upon converting the
elements into strings, then comparing their
sequences of UTF-16 code units values.
High-order methods

forEach()
The forEach() method of Array instances
executes a provided function once for each
array element.

You might also like