Javascript
Apr 29, 2026
What does this print?
const arr = [1, 2, 3]
console.log(arr.indexOf(2))
console.log(arr.indexOf(5))
Explanation
indexOf returns the position (index) of the item you are looking for, starting from 0. So 2 is at position 1. If the item is not found, indexOf returns -1. Many developers use -1 as the "not found" signal to write conditions like: if (arr.indexOf(x) !== -1).
📝
Reviewed by CodeShot Editorial
Every challenge is code-reviewed by senior developers to ensure accuracy and real-world relevance. Learn more.