JS: Array.prototype.indexOf

By Xah Lee. Date: . Last updated: .
arrayX.indexOf(value)
  • Return the index of first element that is value.
  • Return -1 if not found.

🛑 WARNING: NaN is never found. To find NaN, use Array.prototype.includes.

console.log( [3,4,5,6].indexOf(5) === 2);
console.log( ["b", 5, 6, "b"].indexOf ( "b", 1 ) === 3 );
console.log( [3,3].indexOf (3,-1) === 1 );
console.log( [NaN].indexOf ( NaN ) === -1);
console.log( [undefined].indexOf( undefined ) === 0);
arrayX.indexOf(value, startIndex)

Search starts at startIndex.

JavaScript, Array Search