Featured Article

Master Javascript Indexof Examples With Practical Code Snippets

Adam Jul 13, 2026

The JavaScript indexOf() method is an ubiquitous utility that comes in handy when working with arrays, providing a means to determine the first index at which a specified value can be found in the original array, starting from a given index position. If the value is not found, it returns -1. Let's dive deeper into this versatile function, complete with examples that illustrate its practical use.

Java script string method
Java script string method

indexOf()'s primary syntax includes the array and the value to search (value), and optionally, an index from which the search should start (start). The function's simplicity belies its power in simplifying array interactions and enhancing code readability.

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had

Basic indexOf() Usage

The most fundamental use of indexOf() doesn't require a specified start index. It traverses the entire array from the initial position.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Consider this straightforward example:

let fruits = ['apple', 'banana', 'cherry', 'banana', 'mango', 'banana'];
console.log(fruits.indexOf('banana')); // Outputs: 1

Here, 'banana' appears at index positions 1, 3, and 5. However, indexOf() only returns the first occurrence (at index 1).

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

Searching from a Specific Index

In some instances, you may want to start the search from a particular index. This is where the optional second parameter (start) comes into play. It allows you to specify the starting point of the search.

Let's modify the previous example to find the second occurrence of 'banana':

JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡

let fruits = ['apple', 'banana', 'cherry', 'banana', 'mango', 'banana'];
console.log(fruits.indexOf('banana', 2)); // Outputs: 3

In this case, the search begins at index 2 and finds the 'banana' at index 3.

Determining the Absence of a Value

As mentioned, if the target value isn't found, indexOf() returns -1, which provides a convenient way to check if a value exists within an array:

JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝
JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝

Here's an example:

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(numbers.indexOf(11) !== -1); // Outputs: false

Here, we use a not equals operator (!=) to check if 11 isn't present in the array, returning a boolean value.

Essential JavaScript String Methods for Beginners
Essential JavaScript String Methods for Beginners
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
Javascript Fundamental
Javascript Fundamental
Javascript Cheet cheet
Javascript Cheet cheet
the different types of web pages are shown
the different types of web pages are shown
JavaScript Cheat Sheet 🚀 | Complete JS Reference for Beginners & Developers
JavaScript Cheat Sheet 🚀 | Complete JS Reference for Beginners & Developers
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
a diagram showing the different types of data
a diagram showing the different types of data
JavaScript if Statement: Control Program Flow Like a Pro ✅
JavaScript if Statement: Control Program Flow Like a Pro ✅
two different types of font and numbers on the same page, one is for an email
two different types of font and numbers on the same page, one is for an email
JavaScript for Loop: The Ultimate Tool for Repeating Tasks 🔄
JavaScript for Loop: The Ultimate Tool for Repeating Tasks 🔄
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
📘 JavaScript Basics| Linking JS File to HTML | Chapter 2 📖 Part 2
📘 JavaScript Basics| Linking JS File to HTML | Chapter 2 📖 Part 2
JavaScript array methods
JavaScript array methods
JavaScript Basics | Chapter 1 📖 | Part 2
JavaScript Basics | Chapter 1 📖 | Part 2
an info sheet with different types of web pages
an info sheet with different types of web pages
a poster with the names and numbers of different types of programming languages, including an array of
a poster with the names and numbers of different types of programming languages, including an array of
the different types of computers are shown in this diagram, and each one has its own language
the different types of computers are shown in this diagram, and each one has its own language
Java Cheatsheet
Java Cheatsheet

Working with Arrays Within Arrays

indexOf() can also be used to find arrays within arrays:

For example:

let nested = [[1, 2], [3, 4], [5, 6], [7, 8]];
console.log(nested.indexOf([5, 6])); // Outputs: 2

Visualize the array as a matrix, the search result suggests that '[5, 6]' is located at the 2nd row (or index 2).

Remembering indexOf()'s Limitations

While indexOf() is highly useful, it has its limitations. It's important to note that it only returns the position of the first match, potentially skewing results when the same values appear multiples times:

Consider this:

let words = ['apple', 'banana', 'cherry', 'banana', 'mango', 'banana'];
console.log(words.indexOf('banana')); // Outputs: 1

Despite multiple 'banana' instances, indexOf() only returns the first position.

The Magic of findIndex()

If you're looking to find all indices of a target within an array, use the findIndex() method with a helper function inside a loop. This enables you to action on each iteration as needed:

Let's find all instances of 'banana' and log them:

let words = ['apple', 'banana', 'cherry', 'banana', 'mango', 'banana'];
let pos = -1;
while ((pos = words.indexOf('banana', pos + 1)) != -1) {
  console.log(`'banana' found at index ${pos}`);
}

fins and logs each 'banana' occurrence.

While 'indexOf()' is useful, it's not a silver bullet for all array tasks. It often uses with other methods or built-in array utilities to achieve the desired effect. Regardless, it has a vital role in enhancing array manipulation and reading in your codebase.

As you continue to craft JavaScript functionality or debug existing code, remember to leverage the power of 'indexOf()'. It's a fundamental function in your toolkit, ready to help simplify complex array interactions. Until next time, happy coding!