JavaScript Array.indexOf — What does this print?
This is a daily Javascript challenge from the CodeShot archive. Practice your knowledge of Indexof Returns Minus One and improve your technical interview readiness.
const arr = [1, 2, 3]
console.log(arr.indexOf(2))
console.log(arr.indexOf(5))
Detailed Explanation
Why This Question Matters
If you've been coding in JavaScript for a while, indexOf feels like a basic tool. You use it, it works, and you move on. But for developers starting out—or those coming from languages with different collection behaviors—the way JavaScript handles "not found" states can be a silent killer in a codebase.
The confusion usually stems from how we perceive "nothing" or "empty" in programming. In some languages, searching for a missing element might throw an error or return null. JavaScript does neither. It returns a specific integer that, if you aren't careful, can lead to some very weird bugs when used inside a conditional statement.
Understanding the Code
Let's look at the snippet:
Here is what's happening under the hood. The indexOf() method scans the array from left to right (index 0 upwards). It uses strict equality (===) to compare the search element with the items in the array.
1. The first call: arr.indexOf(2)
The engine looks at index 0: is it 2? No, it's 1.
It moves to index 1: is it 2? Yes.
It immediately returns the index: 1.
2. The second call: arr.indexOf(5)
The engine checks index 0, 1, and 2. None of them are 5.
Once it hits the end of the array without a match, it doesn't return undefined or null. It returns -1.
Finding the Correct Answer
The output of this code is:
1
-1
This matches Option B.
Why not other options? Some might guess 0 for the second call because they confuse "not found" with a "zero" value. Others might expect undefined because that's what happens when you try to access an index that doesn't exist (e.g., arr[10]).
But indexOf is a method designed to return a position. Since 0 is a valid position (the first element), the language designers needed a way to signal "failure" using a number that could never be a valid index. Since arrays can't have negative indices, -1 became the universal signal for "not found."
Common Mistakes Developers Make
The biggest mistake I see is using indexOf inside an if statement without checking for -1 explicitly.
Look at this common blunder:
If search is 'dan', indexOf returns -1. In JavaScript, -1 is a truthy value. The code will print "User found!" even though Dan isn't in the list.
Conversely, if the user is 'alice', indexOf returns 0. In JavaScript, 0 is falsy. The code will skip the block, telling you Alice wasn't found even though she's right there at the start of the array.
The fix: Always compare the result to -1.
if (users.indexOf(search) !== -1) { ... }
Real-World Usage
In modern production code, you'll see indexOf less often than you used to. ES6 introduced includes(), which returns a boolean (true or false).
However, indexOf is still essential when you actually need the position of the element. For example, if you need to remove a specific item from an array:
You'll also see this pattern in legacy codebases or when working with strings, as String.prototype.indexOf() behaves exactly the same way.
Key Takeaways
- indexOf returns the first index where the element is found.
- It uses strict equality (===), so it won't coerce types (e.g., searching for "2" in [2, 3] will return -1).
- If the element isn't there, it returns -1, not null or undefined.
- Never use the return value of indexOf as a boolean. Always check !== -1.
- Use includes() if you only care whether the item exists and don't need its position.
Why this matters
Understanding Indexof Returns Minus One is crucial for passing technical interviews. In real-world applications, this concept often leads to subtle bugs if not handled correctly. For more details, you can always refer to the official MDN Documentation.