JavaScript: Array.prototype.findIndex

By Xah Lee. Date: . Last updated: .

New in ES2015.

Returns 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 JavaScript: Keyword “this”

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. JavaScript: Array.prototype.indexOf
  2. JavaScript: Array.prototype.lastIndexOf
  3. JavaScript: Array.prototype.find
  4. JavaScript: Array.prototype.findIndex
  5. JavaScript: Array.prototype.includes

Array Topic

  1. JavaScript: Array Basics
  2. JavaScript: Understand JS Array
  3. JavaScript: Create Array
  4. JavaScript: Sparse Array
  5. JavaScript: What's Array-Like Object?
  6. JavaScript: How to Convert Array-Like Object to Array?
  7. JavaScript: Array How-To

  1. JavaScript: Array Object
  2. JavaScript: Array.prototype
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.