JavaScript: Array.prototype.indexOf

By Xah Lee. Date: . Last updated: .

myArray.indexOf(value)

myArray.indexOf(value, start_index)

Return the index of first element that is value. Search starts at index start_index (default to 0), from left to right.

Return -1 if not found.

var rr = [3,4,5,6];
console.log ( rr.indexOf ( 5 ) ); // 2
var rr = [4, 5, 6, "b"];
console.log ( rr.indexOf ( "b", 1 ) ); // 3

Reference

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

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: Array-Like Object
  6. 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.