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 infoAdvertise with us H HGaur Follow Improve Article Tags : Misc JavaScript Web Technologies javascript-array Explore JavaScript BasicsIntroduction to JavaScript4 min readJavaScript Versions2 min readHow to Add JavaScript in HTML Document?3 min readJavaScript Syntax6 min readJavaScript Output4 min readJavaScript Comments2 min readVariables & DatatypesVariables and Datatypes in JavaScript6 min readGlobal and Local variables in JavaScript4 min readJavaScript Let6 min readJavaScript const5 min readJavaScript Var Statement7 min readOperatorsJavaScript Operators5 min readOperator precedence in JavaScript2 min readJavaScript Arithmetic Operators5 min readJavaScript Assignment Operators5 min readJavaScript Comparison Operators5 min readJavaScript Logical Operators5 min readJavaScript Bitwise Operators5 min readJavaScript Ternary Operator4 min readJavaScript Comma Operator2 min readJavaScript Unary Operators4 min readJavaScript in and instanceof operators3 min readJavaScript String Operators3 min readStatementsJavaScript Statements4 min readJavaScript if-else3 min readJavaScript switch Statement4 min readJavaScript Break Statement2 min readJavaScript Continue Statement1 min readJavaScript Return Statement4 min readLoopsJavaScript Loops3 min readJavaScript For Loop4 min readJavaScript While Loop3 min readJavaScript For In Loop3 min readJavaScript for...of Loop3 min readJavaScript do...while Loop4 min readPerformance & DebuggingJavaScript | Performance4 min readDebugging in JavaScript4 min readJavaScript Errors Throw and Try to Catch2 min readObjectsObjects in Javascript4 min readObject Oriented Programming in JavaScript3 min readJavaScript Objects6 min readCreating objects in JavaScript5 min readJavaScript JSON Objects3 min readJavaScript Object Reference4 min readFunctionsFunctions in JavaScript5 min readHow to write a function in JavaScript ?4 min readJavaScript Function Call2 min readDifferent ways of writing functions in JavaScript3 min readDifference between Methods and Functions in JavaScript3 min readExplain the Different Function States in JavaScript3 min readJavaScript Function Complete Reference3 min readArraysJavaScript Arrays7 min readJavaScript Array Methods7 min readBest-Known JavaScript Array Methods6 min readImportant Array Methods of JavaScript7 min readJavaScript Array Reference4 min readStringJavaScript Strings6 min readJavaScript String Methods9 min readJavaScript String Reference4 min read Like