JavaScript indexOf() method in an Object Array Last Updated : 14 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, indexOf() methods provide the first index at which the given element exists, and -1 in case it is not present in the array. Syntax: indexOf(element)indexOf(element, start);Parameters: element: the element to be searched in the input arraystart: the index from which the search has to begin. The default value is set to 0.Return value: JavaScript indexOf() method returns the value of the index at which the element is present, and -1 when the element doesn't exist. JavaScript indexOf() method in an Object Array ExamplesExample 1: Basic Usage In this example, the indexOf() method returns -1 because the object { name: 'banana', color: 'yellow' } is a different object instance than the one stored in the array. JavaScript let fruits = [ { name: 'apple', color: 'red' }, { name: 'banana', color: 'yellow' }, { name: 'orange', color: 'orange' } ]; // Find the index of an object in the array let index = fruits.indexOf({ name: 'banana', color: 'yellow' }); console.log(index); // Output: -1 (Not found) Output-1 Example 2: Index of Modified Object In this case, the modified banana object is added to the array, and then indexOf() method returns the index where this modified object is located. JavaScript // Example Title: Finding Index of a Modified Object // Array of objects representing fruits let fruits = [ { name: 'apple', color: 'red' }, { name: 'banana', color: 'yellow' }, { name: 'orange', color: 'orange' } ]; let banana = { name: 'banana', color: 'yellow' }; // Finding the index of an object after modifying it fruits.push(banana); // Adding the modified object to the array let index = fruits.indexOf(banana); console.log(index); // Output: 3 Output3 Comment More infoAdvertise with us P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-array javascript-object JavaScript-Questions +1 More Explore JavaScript Tutorial 8 min read JavaScript BasicsIntroduction to JavaScript 4 min read JavaScript Versions 2 min read How to Add JavaScript in HTML Document? 3 min read JavaScript Syntax 6 min read JavaScript Output 4 min read JavaScript Comments 2 min read JS Variables & DatatypesVariables and Datatypes in JavaScript 6 min read Global and Local variables in JavaScript 4 min read JavaScript Let 6 min read JavaScript const 5 min read JavaScript Var Statement 7 min read JS OperatorsJavaScript Operators 5 min read Operator precedence in JavaScript 2 min read JavaScript Arithmetic Operators 5 min read JavaScript Assignment Operators 5 min read JavaScript Comparison Operators 5 min read JavaScript Logical Operators 5 min read JavaScript Bitwise Operators 5 min read JavaScript Ternary Operator 4 min read JavaScript Comma Operator 2 min read JavaScript Unary Operators 4 min read JavaScript in and instanceof operators 3 min read JavaScript String Operators 3 min read JS StatementsJavaScript Statements 4 min read JavaScript if-else 3 min read JavaScript switch Statement 4 min read JavaScript Break Statement 2 min read JavaScript Continue Statement 1 min read JavaScript Return Statement 4 min read JS LoopsJavaScript Loops 3 min read JavaScript For Loop 4 min read JavaScript While Loop 3 min read JavaScript For In Loop 3 min read JavaScript for...of Loop 3 min read JavaScript do...while Loop 4 min read JS Perfomance & DebuggingJavaScript | Performance 4 min read Debugging in JavaScript 4 min read JavaScript Errors Throw and Try to Catch 2 min read JS ObjectObjects in Javascript 4 min read Object Oriented Programming in JavaScript 3 min read JavaScript Objects 6 min read Creating objects in JavaScript 5 min read JavaScript JSON Objects 3 min read JavaScript Object Reference 4 min read JS FunctionFunctions in JavaScript 4 min read How to write a function in JavaScript ? 4 min read JavaScript Function Call 2 min read Different ways of writing functions in JavaScript 3 min read Difference between Methods and Functions in JavaScript 3 min read Explain the Different Function States in JavaScript 3 min read JavaScript Function Complete Reference 3 min read JS ArrayJavaScript Arrays 7 min read JavaScript Array Methods 7 min read Best-Known JavaScript Array Methods 6 min read Important Array Methods of JavaScript 7 min read JavaScript Array Reference 4 min read Like