JavaScript Array indexOf()

indexOf() searches an array from left to right and returns the index of the first element that matches the value you provide. If no matching element is found, it returns -1.

const languages = ["PHP", "ASP", "JavaScript", "HTML"];
console.log(languages.indexOf("HTML")); // 3

Array indexes start at 0, so "HTML" is at index 3 in this example.

JavaScript indexOf() Syntax and Return Value Top ↑

array.indexOf(searchElement)
array.indexOf(searchElement, fromIndex)

searchElement is the value to find. The optional fromIndex tells JavaScript where to begin searching. The search still moves forward from that point.

Return value: the first matching zero-based index, or -1 when no match exists.

When indexOf() Does Not Find the Value Top ↑

const languages = ["PHP", "ASP", "JavaScript", "HTML"];
const position = languages.indexOf("SQL");
console.log(position); // -1

Testing explicitly against -1 is useful when you need the position itself.

if (languages.indexOf("JavaScript") !== -1) {
  console.log("JavaScript is in the array");
}

Duplicate Values: indexOf() Returns the First Match Top ↑

If the same value appears more than once, indexOf() returns the first matching position from the start of the search.

const scripts = ["PHP", "ASP", "JavaScript", "HTML", "ASP"];
console.log(scripts.indexOf("ASP")); // 1

This preserves the original page example: the second "ASP" is at index 4, but the first match is index 1.

Start Searching from a Specific Index Top ↑

const scripts = ["PHP", "ASP", "JavaScript", "HTML", "ASP"];
console.log(scripts.indexOf("ASP", 2)); // 4

The second argument does not mean “occurrence number.” It is the array index where the forward search begins.

Using a Negative fromIndex Top ↑

A negative fromIndex counts from the end to determine the starting position, but the search still proceeds toward the end of the array.

const values = ["a", "b", "c", "b"];
console.log(values.indexOf("b", -2)); // 3
console.log(values.indexOf("a", -2)); // -1

indexOf() vs lastIndexOf() Top ↑

lastIndexOf() searches backward and returns the last matching index. This is useful when duplicate values are present.

const scripts = ["PHP", "ASP", "JavaScript", "HTML", "ASP"];
console.log(scripts.indexOf("ASP"));     // 1
console.log(scripts.lastIndexOf("ASP")); // 4

indexOf() vs includes() Top ↑

Use indexOf() when you need the numeric position. Use includes() when you only need a Boolean answer.

const roles = ["admin", "editor", "viewer"];
console.log(roles.indexOf("editor")); // 1
console.log(roles.includes("editor")); // true

Strict Equality, Objects and NaN Top ↑

indexOf() uses strict-equality style matching. Different object instances do not match just because their contents look the same.

const user = { id: 1 };
const users = [user];

console.log(users.indexOf(user));       // 0
console.log(users.indexOf({ id: 1 })); // -1

NaN is another important edge case: indexOf(NaN) returns -1. If you only need to know whether NaN is present, includes(NaN) can find it.

const numbers = [10, NaN, 30];
console.log(numbers.indexOf(NaN)); // -1
console.log(numbers.includes(NaN)); // true

Finding Every Occurrence of a Value Top ↑

const letters = ["a", "b", "a", "c", "a"];
const indexes = [];
let index = letters.indexOf("a");

while (index !== -1) {
  indexes.push(index);
  index = letters.indexOf("a", index + 1);
}

console.log(indexes); // [0, 2, 4]

Safely Removing a Found Item Top ↑

Check that the returned index is not -1 before passing it to splice(). Otherwise splice(-1, 1) removes the final element.

const scripts = ["PHP", "ASP", "JavaScript", "HTML"];
const index = scripts.indexOf("ASP");

if (index !== -1) {
  scripts.splice(index, 1);
}

console.log(scripts); // ["PHP", "JavaScript", "HTML"]

Common indexOf() Mistakes Top ↑

  • Expecting a Boolean result instead of an index or -1.
  • Forgetting that matching is case-sensitive for strings.
  • Using if (array.indexOf(value)); index 0 is falsy and -1 is truthy.
  • Expecting separate object literals with the same properties to match.
  • Using indexOf() to find NaN.
  • Passing -1 directly to splice() after an unsuccessful search.

Array Reference Sort Array Elements




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer