Science Knowings: JavaScript Course For Social Media

Array.findIndex() Method

Array.findIndex() Method

The findIndex() method in JavaScript is an array method used to find the index of the first element in an array that satisfies a given predicate function. Unlike the find() method, findIndex() returns the index of the element instead of the element itself.

Return Value of findIndex()

The findIndex() method returns the index of the first element that satisfies the predicate function or -1 if no element satisfies the condition.

Syntax of findIndex()

findIndex((element, index, array) => { /* predicate function */ })

Parameters of findIndex()

  • element: The current element being processed in the array.
  • index (optional): The index of the current element.
  • array (optional): The array the findIndex() method is applied on.

Callback Function of findIndex()

The callback function of findIndex() takes three arguments: element, index, and array, and returns a Boolean value. If the callback function returns true for an element, the findIndex() method stops iterating the array and returns the index of that element.

thisArg of findIndex()

The thisArg parameter is optional and specifies the value to use as this when executing the callback function. If thisArg is not provided, undefined is used as the this value.

findIndex() vs find()

The findIndex() method is similar to the find() method, but there are a few key differences:

  • findIndex() returns the index of the first element that satisfies the predicate function, while find() returns the element itself.
  • findIndex() stops iterating the array as soon as it finds an element that satisfies the condition, while find() iterates the entire array.

findIndex() Examples

null

Example 1: Finding the Index of a Value in an Array

const colors = ['red', 'green', 'blue'];
const indexOfGreen = colors.findIndex(color => color === 'green');

console.log(indexOfGreen); // expected output: 1

Example 2: Finding the Index of an Object in an Array

const users = [
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 40 },
  { name: 'Charlie', age: 50 }
];

const indexOfAlice = users.findIndex(user => user.name === 'Alice');

console.log(indexOfAlice); // expected output: 0

Example 3: Finding the Index of a Number in a Sorted Array

const numbers = [1, 3, 5, 7, 9];
const indexOf5 = numbers.findIndex(number => number >= 5);

console.log(indexOf5); // expected output: 2

Example 4: Finding the Index of the First Element That Satisfies a Condition

const scores = [80, 95, 75, 90, 100];
const firstScoreGreaterThan90 = scores.findIndex(score => score > 90);

console.log(firstScoreGreaterThan90); // expected output: 3

Example 5: Using findIndex() with Arrow Functions

const numbers = [1, 3, 5, 7, 9];
const indexOf5 = numbers.findIndex(number => number === 5);

console.log(indexOf5); // expected output: 2

Applications of findIndex()

  • Finding the first matching element in an array.
  • Determining if an element exists in an array.
  • Getting the index of the first element that meets a specific condition.
  • Searching in a sorted array using binary search.

Next Topic: Object.freeze() Method

The Object.freeze() method in JavaScript is used to make an object immutable. Once an object is frozen, its properties cannot be added, removed, or modified. This is useful for creating objects that cannot be accidentally changed.

Why Learn Object.freeze() Method?

Follow us to learn why understanding the Object.freeze() method is essential for writing robust and secure JavaScript applications.