Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Array Search

Array Find and Search Methods encompass functions such as indexOf(), lastIndexOf(), and includes() for locating elements within arrays.

Additionally, see the related sections on Basic Methods, Sort Methods, and Iteration Methods.

JavaScript Array indexOf()

The indexOf() method scans an array for a specified element value and returns its position.

Note: The first item occupies position 0, the second item occupies position 1, and so forth.

Example

Look for the item “Apple” within the array.

const fruits = [“Apple”“Orange”“Apple”“Mango”];
let position = fruits.indexOf(“Apple”) + 1;

Syntax

array.indexOf(item, start)

item

Mandatory. The item to be searched for.

start

Optional. Indicates the starting point for the search. Negative values will initiate the search from the specified position counting from the end, and continue until the end of the array.

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

If the item is present multiple times, it returns the position of the first occurrence.

JavaScript Array lastIndexOf()

Array.lastIndexOf() functions similarly to Array.indexOf(), but it returns the position of the last occurrence of the specified element.

Example

Look for the item “Apple” within the array.

const fruits = [“Apple”“Orange”“Apple”“Mango”];
let position = fruits.lastIndexOf(“Apple”) + 1;

Syntax

array.lastIndexOf(item, start)

item

Mandatory. The item to be searched for.

start

Optional. Specifies the starting point for the search. Negative values will initiate the search from the specified position counting from the end, and continue towards the beginning of the array.

JavaScript Array includes()

In ECMAScript 2016, Array.includes() was introduced to arrays. This method enables us to determine if an element is included in an array, including the ability to check for NaN, unlike indexOf().

Example

const fruits = [“Banana”“Orange”“Apple”“Mango”];

fruits.includes(“Mango”); // is true

Syntax

array.includes(search-item)
Array.includes() permits checking for NaN values, which differs from Array.indexOf().

Browser Support

The includes() method is a feature introduced in ECMAScript 2016.

ES2016 is fully supported in all modern browsers as of March 2017.

IMG_4151

includes() is not compatible with Internet Explorer.

JavaScript Array find()

The find() method retrieves the value of the initial array element that satisfies a specified test function.

In this example, it identifies (and returns the value of) the first element greater than 18:

Example

const numbers = [49162529];
let first = numbers.find(myFunction);

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

Note that the function accepts three arguments:

  • The value of the item
  • The index of the item
  • The array itself

Browser Support

The find() method is an ES6 feature (JavaScript 2015).

ES6 has been fully supported in all modern browsers since June 2017.

IMG_4152

find() is not compatible with Internet Explorer.

JavaScript Array findIndex()

The findIndex() method retrieves the index of the initial array element that satisfies a specified test function.

In this example, it identifies the index of the first element greater than 18:

Example

const numbers = [49162529];
let first = numbers.findIndex(myFunction);

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

Please note that the function accepts three arguments:

  • The value of the item
  • The index of the item
  • The array itself

Browser Support

The findIndex() method is an ES6 feature (JavaScript 2015).

ES6 has been fully supported in all modern browsers since June 2017.

IMG_4152

findIndex() is not compatible with Internet Explorer.

JavaScript Array findLast() Method

In ES2023, the findLast() method was introduced. It begins searching from the end of an array and returns the value of the first element that meets a specified condition.

Example

const temp = [27283040423530];
let high = temp.findLast(x => x > 40);

Browser Support

findLast() is a feature introduced in ES2023.

It has been supported in all modern browsers since July 2023.

IMG_4152

JavaScript Array findLastIndex() Method

The findLastIndex() method locates the index of the last element that meets a specified condition.

Example

const temp = [27283040423530];
let pos = temp.findLastIndex(x => x > 40);

Browser Support

findLastIndex() is a feature introduced in ES2023.

It has been supported in all modern browsers since July 2023.

IMG_4154