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. Returns 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

Reference

ECMAScript® 2016 Language Specification#sec-array.prototype.findindex

Array Search Topic

  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

Array Topic

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

  1. JS: Array Object
  2. JS: Array.prototype
Like what you read? Buy JavaScript in Depth