JS: Array.prototype.findIndex

By Xah Lee. Date: . Last updated: .

New in ES2015.

Return the first found index of a value of array myArray where the function f returns true.

Return undefined if not found.

If thisArg is given, it will be used as this value of f. If it is not given, undefined is used.

[see JS: “this” Binding]

f must be a function. The function should return true or false.

The function f is passed 3 args: • current_elementcurrent_indexmyArray.

const isEven = (x => ( x % 2 === 0 ? true : false ) )

console.log( [7,3,4,2].findIndex (isEven) ); // 2

// if not found, return undefined
console.log( [1,3,5].find (isEven) ); // undefined

JS Array Search

  1. JS: Array.prototype.indexOf
  2. JS: Array.prototype.lastIndexOf
  3. JS: Array.prototype.find
  4. JS: Array.prototype.findIndex
  5. JS: Array.prototype.includes

JS Array

  1. Understand JS Array
  2. Create Array
  3. Sparse Array
  4. Array-Like Object
  5. Array How-To

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials