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.
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.
-1 when no match exists.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");
}
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.
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.
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
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
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
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
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]
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"]
-1.if (array.indexOf(value)); index 0 is falsy and -1 is truthy.indexOf() to find NaN.-1 directly to splice() after an unsuccessful search.Array Reference Sort Array Elements
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.