W3Schools
w3schools.com › jsref › jsref_indexof_array.asp
JavaScript Array indexOf() Method
In an array, the first element has index (position) 0, the second has index 1, ... Find the first index of "Apple", starting from the last element: const fruits = ["Banana", "Orange", "Apple", "Mango", "Apple"]; let index = fruits.indexOf("Apple", ...
W3Schools
w3schools.com › js › js_array_search.asp
JavaScript Array Search
JavaScript Array Reference · The indexOf() method searches an array for an element value and returns its position. Note: The first item has position 0, the second item has position 1, and so on.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › indexOf
Array.prototype.indexOf() - JavaScript | MDN
The first index of searchElement in the array; -1 if not found. The indexOf() method compares searchElement to elements of the array using strict equality (the same algorithm used by the === operator).
W3Schools Blog
w3schools.blog › home › javascript array indexof()
JavaScript Array indexOf() - W3schools
April 20, 2019 - The JavaScript array indexOf() method is used to search the specified element in the given array and returns the index of the first match.
W3Schools
w3schools.com › jsref › tryit.asp
The indexOf() Method - JavaScript Arrays
The W3Schools online code editor allows you to edit code and view the result in your browser
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.indexof()
JavaScript Array indexOf() Method
November 6, 2024 - This tutorial shows you how to use the JavaScript Array indexOf() method to find the first index of the element.
W3Schools
w3schools.com › jsref › jsref_findindex.asp
JavaScript Array findIndex() Method
Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean ·
Programiz
programiz.com › javascript › library › array › indexof
JavaScript Array indexOf()
var index4 = priceList.indexOf(69.5); console.log(index4); // -1 ... If fromIndex >= array.length, array is not searched and -1 is returned.
Top answer 1 of 16
1394
I think you can solve it in one line using the map function:
const pos = myArray.map(e => e.hello).indexOf('stevie');
2 of 16
541
Array.prototype.findIndex is supported in all browsers other than IE (non-edge). But the polyfill provided is nice.
var indexOfStevie = myArray.findIndex(i => i.hello === "stevie");
The solution with map is okay. But you are iterating over the entire array every search. That is only the worst case for findIndex which stops iterating once a match is found.
There's not really a concise way (when devs had to worry about IE8), but here's a common solution:
var searchTerm = "stevie",
index = -1;
for(var i = 0, len = myArray.length; i < len; i++) {
if (myArray[i].hello === searchTerm) {
index = i;
break;
}
}
or as a function:
function arrayObjectIndexOf(myArray, searchTerm, property) {
for(var i = 0, len = myArray.length; i < len; i++) {
if (myArray[i][property] === searchTerm) return i;
}
return -1;
}
arrayObjectIndexOf(arr, "stevie", "hello"); // 1
Just some notes:
- Don't use for...in loops on arrays
- Be sure to break out of the loop or return out of the function once you've found your "needle"
- Be careful with object equality
For example,
var a = {obj: 0};
var b = [a];
b.indexOf({obj: 0}); // -1 not found
W3Schools
w3schools.com › js › tryit.asp
W3Schools online HTML editor
The W3Schools online code editor allows you to edit code and view the result in your browser
W3Schools
w3schools.com › jsref › jsref_indexof.asp
JavaScript String indexOf() Method
Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean ·
MDN
developer.mozilla.org.cach3.com › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › indexOf$revision › 1029062
Revision 1029062 | Array.prototype.indexOf() | MDN
If the calculated index is less than 0, then the whole array will be searched. Default: 0 (entire array is searched). indexOf() compares searchElement to elements of the Array using strict equality (the ...
DigitalOcean
digitalocean.com › community › tutorials › js-indexof
Exploring the indexOf Method for Strings and Arrays in JavaScript | DigitalOcean
December 17, 2019 - In its simplest version, the indexOf method takes one argument which is the element we are trying to find. Then it returns the index of the first element it finds in the array which satisfies el === target. This means that even if there are two matches in your array, indexOf will only return ...
Codecademy
codecademy.com › docs › javascript › arrays › .indexof()
JavaScript | Arrays | .indexOf() | Codecademy
June 12, 2025 - 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.
Mimo
mimo.org › glossary › javascript › array-indexof-method
JavaScript Array indexOf() method: Syntax, Usage, and Examples
The indexOf() method finds the first index of a specified element in an array. If the element is not found in the array, it returns -1. This is most commonly used to check if an element exists or to get its position. ... Become a full-stack developer. Learn HTML, CSS, JavaScript, and React ...
W3Schools
w3schools.com › jsref › tryit.asp
The indexOf() Method - JavaScript Strings
The W3Schools online code editor allows you to edit code and view the result in your browser
W3Schools
w3schools.com › jsref › jsref_lastindexof_array.asp
JavaScript Array lastIndexOf() Method
The lastIndexOf() starts at a specified index and searches from right to left (from the given postion to the beginning of the array).
TutorialsPoint
tutorialspoint.com › home › javascript › javascript array indexof method
JavaScript Array indexOf Method Explained
September 1, 2008 - Learn how to use the JavaScript Array indexOf method to find the index of an element within an array. Understand its syntax, parameters, and return values.
W3Schools Blog
w3schools.blog › home › javascript array
JavaScript Array - W3schools
April 20, 2019 - Array indexOf() method: Use: To search the specified element in the given array and return the index of the first match.