JavaScript .indexOf()
The .indexOf() method returns the first index at which a specified element can be found in an array, or -1 if the element is not present. This method searches the array from left to right and uses strict equality comparison to match elements.
The .indexOf() method is commonly used for element detection, data validation, conditional logic, and implementing search functionality in web applications. It is a fundamental tool for determining whether specific values exist in arrays and their exact positions. It is essential for user input validation, menu systems, and data processing workflows.
Syntax
array.indexOf(searchElement, fromIndex)
Parameters:
searchElement: The element to search for in the array.fromIndex(Optional): The index position to start the search from. The default value is 0. Negative values count back from the end of the array, but still search from left to right.
Return value:
The .indexOf() method returns a number representing the index position of the first occurrence of the specified element, or -1 if the element is not found.
Example 1: Basic Usage of the .indexOf() Method
This example demonstrates the fundamental usage of the indexOf() method to locate elements in an array:
const fruits = ['apple', 'banana', 'orange', 'grape'];// Find the index of "banana"const bananaIndex = fruits.indexOf('banana');console.log(bananaIndex);// Search for an element that doesn't existconst kiwiIndex = fruits.indexOf('kiwi');console.log(kiwiIndex);// Check if an element exists in the arrayif (fruits.indexOf('orange') !== -1) {console.log('Orange is in the array');} else {console.log('Orange is not in the array');}
The output of this code will be:
1-1Orange is in the array
This example shows how .indexOf() returns the zero-based index position of the first matching element. When “banana” is found at position 1, the method returns 1. When searching for “kiwi”, which doesn’t exist, it returns -1. The conditional check demonstrates a common pattern for testing the existence of an element.
Example 2: Product Inventory Management
The following example shows how .indexOf() can be used in a real-world inventory management system to track product availability and prevent duplicates:
const inventory = ['laptop', 'mouse', 'keyboard', 'monitor', 'speakers'];// Function to check if a product is in stockfunction checkStock(product) {const index = inventory.indexOf(product);if (index !== -1) {return `${product} is in stock at position ${index}`;} else {return `${product} is out of stock`;}}// Function to add new product if not already presentfunction addProduct(product) {if (inventory.indexOf(product) === -1) {inventory.push(product);console.log(`${product} added to inventory`);} else {console.log(`${product} already exists in inventory`);}}// Test the functionsconsole.log(checkStock('mouse'));console.log(checkStock('tablet'));addProduct('webcam');addProduct('laptop');console.log(inventory);
The output of this code will be:
mouse is in stock at position 1tablet is out of stockwebcam added to inventorylaptop already exists in inventory[ 'laptop', 'mouse', 'keyboard', 'monitor', 'speakers', 'webcam' ]
This example demonstrates practical usage where .indexOf() helps maintain inventory integrity by preventing duplicate entries and quickly checking product availability. The functions show how to combine .indexOf() with conditional logic for business operations.
Codebyte Example: User Permission System
This example illustrates using .indexOf() with a starting position parameter to implement a user permission system with role hierarchy:
The above example demonstrates advanced usage of the fromIndex parameter by finding multiple occurrences of a value and implementing a role-based permission system. The while loop demonstrates how to find all instances of an element by repeatedly calling .indexOf() with updated starting positions.
Frequently Asked Questions
1. What happens when .indexOf() searches for objects or arrays?
The .indexOf() method uses strict equality comparison, which only matches the same object reference, not objects with identical content. For content-based matching of objects, use findIndex() with a custom comparison function.
2. Can .indexOf() work with negative starting positions?
Yes, negative values for the fromIndex parameter count backwards from the end of the array. However, the search still proceeds from left to right. If the calculated starting position is negative, the search begins from index 0.
3. How does .indexOf() handle undefined and null values?
The method can find undefined and null values in arrays using strict equality. It will return the index position of these values if they exist, or -1 if they don’t.
4. Is .indexOf() case-sensitive when searching strings?
Yes, .indexOf() performs case-sensitive matching for string elements. “Apple” and “apple” are treated as different values.
5. What is the difference between .indexOf() and .lastIndexOf()?
Both .indexOf() and .lastIndexOf() search for a specified element in an array, but they differ in the direction of the search:
.indexOf()searches the array from left to right (starting from the first index)..lastIndexOf()searches the array from right to left (starting from the last index).
All contributors
BrandonDusch
christian.dinh- Anonymous contributor
- robgmerrill
- Anonymous contributor
- MamtaWardhani
- grace_k
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours