Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript Array indexOf() Method

  • 30 Sep, 2025
  • Com 0
JavaScript Array indexOf() Function

JavaScript Array indexOf() is a built-in function that searches for a specified element and returns its position. The position here is the index of the element, and if the element does not exist, it returns -1.

Finding the index of the found element in the JavaScript Array

const festivals = ['holi', 'diwali', 'christmas'];

console.log(festivals.indexOf('diwali'));

// Output: 1

In this code, we are searching for the position of the “diwali” element, and it is at position 1 or index 1. The array index starts with 0.

Syntax

array.indexOf(searchElement, fromIndex)

Parameters

Argument Description
searchElement (required) It represents the value to search for in the array.
fromIndex (optional) It represents an integer specifying the index at which to begin the search.

By default, its value is 0 if it is omitted.

Non-integer values are coerced to integers (e.g., 1.5 becomes 1).

Element does not exist

If the element’s position you are trying to locate does not exist, it returns -1.

Element does not exist in JavaScript Array

const festivals = ['holi', 'diwali', 'christmas'];

console.log(festivals.indexOf('rakhi'));

// Output: -1

You can use this approach, which allows conditional checks like if (festivals.indexOf(‘rakhi’) !== -1), to verify the existence of an input element.

Multiple occurrences

If multiple elements with the same value exist in the array being searched, it only returns the first index.

Multiple occurrences of a single element in the array

const festivals = ['holi', 'diwali', 'christmas', 'diwali'];

console.log(festivals.indexOf('diwali'));

// Output: 1

The “diwali” exists at positions 1 and 3, but it only returns the index of the first element, and hence it returns 1 in the output.

Using fromIndex (Start mid-array)

If you want to skip the earlier matches, you can use the fromIndex argument. For example, if you wish to get the second occurrence’s index, you can pass the fromIndex argument.

Using fromIndex (Start mid-array)

const festivals = ['holi', 'diwali', 'christmas', 'diwali'];

console.log(festivals.indexOf('diwali', 2));

// Output: 3

Negative fromIndex

If fromIndex is negative, it is interpreted as: startIndex = array.length + fromIndex

For example, if the array length is 4 and fromIndex is -1, the startIndex should be 4-1 = 3.

Negative fromIndex in array indexOf() method

const festivals = ['holi', 'diwali', 'christmas', 'diwali'];

console.log(festivals.indexOf('diwali', -1));

// Output: 3

In this code, we started searching from index 3, and at the same position, “diwali” was found, so it returns 3 as the output.

fromIndex greater than array length

If the fromIndex is greater than the array length, it returns -1 because no search occurs, and hence no element is found.

const festivals = ['holi', 'diwali', 'christmas', 'diwali'];

console.log(festivals.indexOf('diwali', 5));

// Output: -1

Searching for NaN

You can’t search for a NaN value in the array because NaN !== NaN. Use an array.some(x => Number.isNaN(x)) instead.

const nan_festivals = ['holi', 'diwali', 'christmas', NaN];

console.log(nan_festivals.indexOf(NaN));

// Output: -1

Empty array

If the array is empty, there is nothing to search for, and it returns -1.

const empty_array = [];

console.log(empty_array.indexOf('data'));

// Output: -1

That’s all!

Post Views: 5
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

JavaScript String indexOf() Method
JavaScript String startsWith() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend