JavaScript Array indexOf() Method Last Updated : 27 Sep, 2024 Comments Improve Suggest changes Like Article Like Report The indexOf() method in JavaScript is used to find the position of the first occurrence of a specific value in an array. If the value is not present, it returns -1. This method is handy for quickly determining where a particular item is located within an array.Syntax:array.indexOf(element, start)Parameters:element: This parameter holds the element whose index will be returned.start: This parameter is optional and holds the starting point of the array, where the default value is 0 to begin the search.Return value:The method returns the index of the first occurrence of the specified element.If the element is not found, it returns -1.Example 1: Finding Index of Element in ArrayThis code demonstrates the use of the indexOf() method to find the index of the element "gfg" in the array name. The index of "gfg" is stored in the variable a and then logged into the console. JavaScript let name = ['gfg', 'cse', 'geeks', 'portal']; a = name.indexOf('gfg') // Printing result of method console.log(a) Output0 Example 2: Searching Element in ArrayThis code demonstrates the use of the indexOf() method to find the index of a specific element (2) in an array (A). It returns the index of the first occurrence of the element in the array (1 in this case). If the element is not found, it returns -1. JavaScript // Taking input as an array A // having some elements. let A = [1, 2, 3, 4, 5]; // indexOf() method is called to // test whether the searching element // is present in given array or not. a = A.indexOf(2) // Printing result of method. console.log(a); Output1 We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.Supported Browsers: Google ChromeEdge FirefoxOperaSafari Comment More info H HGaur Follow Improve Article Tags : Misc JavaScript Web Technologies javascript-array Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like