The .indexOf() method has an optional second parameter that specifies the index to start searching from, so you can call it in a loop to find all instances of a particular value:

function getAllIndexes(arr, val) {
    var indexes = [], i = -1;
    while ((i = arr.indexOf(val, i+1)) != -1){
        indexes.push(i);
    }
    return indexes;
}

var indexes = getAllIndexes(Cars, "Nano");

You don't really make it clear how you want to use the indexes, so my function returns them as an array (or returns an empty array if the value isn't found), but you could do something else with the individual index values inside the loop.

UPDATE: As per VisioN's comment, a simple for loop would get the same job done more efficiently, and it is easier to understand and therefore easier to maintain:

function getAllIndexes(arr, val) {
    var indexes = [], i;
    for(i = 0; i < arr.length; i++)
        if (arr[i] === val)
            indexes.push(i);
    return indexes;
}
Answer from nnnnnn on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › find-all-occurrences-of-a-word-in-array-in-javascript
Find all occurrences of a word in array in JavaScript
We are required to write a JavaScript function that takes in an array of literals as the first argument and a string as the second argument. Our function should return the count of the number of times that string (provided by second argument) appears anywhere in the array. ... const arr = ["word", "a word", "another word"]; const query = "word"; const findAll = (arr, query) => { let count = 0; count = arr.filter(el => { return el.indexOf(query) != -1; }).length; return count; }; console.log(findAll(arr, query));
Discussions

Find all occurrences of an element in an array
js-mentorship-razvan / javascript Public · Notifications · You must be signed in to change notification settings · Fork 2 · Star 21 · New issueCopy link · New issueCopy link · Closed · Closed · Find all occurrences of an element in an array#457 · Copy link · Assignees · More on github.com
🌐 github.com
2
April 2, 2020
Search entire array for number of occurrence of index value
I am new to JS and am trying to get a function to take the value of an array (at say index 0) and compare it to another array to find out how many times that value (index 0) is found in the other array, and add that number to a variable (currentCount += 1); then look at the next value (index ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
6
0
December 4, 2017
JavaScript utility method find number of occurrences in an array
202 votes, 26 comments. 275K subscribers in the learnjavascript community. This subreddit is for anyone who wants to learn JavaScript or help others… More on reddit.com
🌐 r/learnjavascript
26
202
January 27, 2021
javascript - Find all occurrences of word in array - Stack Overflow
I'm trying to find the number of total occurrences of a word in an array. Here I found a solution and altered it a bit: var dataset = ["word", "a word", "another word"]; var search = "word"; var c... More on stackoverflow.com
🌐 stackoverflow.com
April 14, 2016
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-find-index-all-occurrences-of-element-in-array
Find the indexes of all Occurrences of Element in JS Array | bobbyhadz
The function we passed to the Array.forEach method gets called with each element in the array. On each iteration, we check if the current element is equal to the specified value. If the condition is met, we push the current index into the indexes ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-find-the-index-of-all-occurrence-of-elements-in-an-array-using-javascript
Find All Occurrence Indices in a JavaScript Array | GeeksforGeeks
January 25, 2025 - The map() and filter() methods to find the index of all occurrences of elements in an array using JavaScript.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › array › count occurrences
Count the occurrences of a value in a JavaScript array or string - 30 seconds of code
May 14, 2024 - Simply increment a counter each time the specific value is encountered inside the array and return the final count. const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0); countOccurrences([1, 1, 2, 1, 2, 3], ...
🌐
GitHub
github.com › js-mentorship-razvan › javascript › issues › 457
Find all occurrences of an element in an array · Issue #457 · js-mentorship-razvan/javascript
April 2, 2020 - js-mentorship-razvan / javascript Public · Notifications · You must be signed in to change notification settings · Fork 2 · Star 21 · New issueCopy link · New issueCopy link · Closed · Closed · Find all occurrences of an element in an array#457 · Copy link · Assignees ·
Author   js-mentorship-razvan
🌐
YouTube
youtube.com › watch
How to Find all occurrences of a value in an array in JavaScript - YouTube
How to Find all occurrences of a value in an array in and return an array of matching indexes in JavaScript.
Published   March 10, 2024
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › count-occurrences-of-all-items-in-an-array-in-javascript
Count Occurrences of All Items in an Array in JavaScript | GeeksforGeeks
For each item, it either increments its count or initializes it to 1 if it's the first occurrence. we are using the _.frequencies() method for calculating the occurance of all elements in the given array.
Published   April 7, 2025
🌐
DEV Community
dev.to › martinkr › array-map-of-occurrences-4ndp
1 line of code: How to count all occurrences in an Array - DEV Community
November 11, 2021 - const occurrenceMap = arr => arr.reduce((acc, current) => (acc[current] = (acc[current] || 0)... Tagged with javascript, webdev, productivity, codequality.
🌐
Medium
medium.com › @atulgairola08 › how-to-find-the-number-of-occurrences-of-each-element-in-an-array-javascript-f3688a663f1
How to find the number of occurrences of each element in an Array — Javascript | by Atul Gairola | Medium
May 28, 2020 - The trackObj now contains each element and the count of the times it has occurred in the array. ... Alright, so now we got an object of the occurrences of all the elements.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › find
Array.prototype.find() - JavaScript - MDN Web Docs
If you need to find if a value exists in an array, use includes(). Again, it checks each element for equality with the value instead of using a testing function. If you need to find if any element satisfies the provided testing function, use some(). If you need to find all elements that satisfy ...
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Search entire array for number of occurrence of index value - Curriculum Help - The freeCodeCamp Forum
December 4, 2017 - I am new to JS and am trying to ... value (index 0) is found in the other array, and add that number to a variable (currentCount += 1); then look at the next value (index ......
🌐
QuickRef.ME
quickref.me › home › how to count the occurrences of a value in an array in javascript - quickref.me
How to count the occurrences of a value in an array in JavaScript - QuickRef.ME
In this Article we will go through how to count the occurrences of a value in an array only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.
🌐
Stack Abuse
stackabuse.com › count-number-of-element-occurrences-in-javascript-array
Count Number of Element Occurrences in JavaScript Array
May 23, 2022 - To accomplish this, we need to set the counter variable to 0, then loop through the array and increase the counter by one each time we find the element we are looking for. When we loop through the entire array, the counter will store the number of occurrences of the element we were searching for:
🌐
Reddit
reddit.com › r › learnjavascript › comments › l6guld › javascript_utility_method_find_number_of
r/learnjavascript on Reddit: JavaScript utility method find number of occurrences in an array
January 27, 2021 - The first line in the function (an arrow function) is getting the current count for the current array value, passed in as value from the Map. ... If the value doesn't exist, this will come back as undefined. Since undefined is not a valid value to use in numeric operations, we default this value to 0 if it returns undefined. We do this using the nullish coalescing operator. This is a fairly new operator in JavaScript, so you might not have seen it before.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-array-find-all
Exploring TypeScript Array Find All Method
In the above code snippet, we have an array of numbers and our target number is 2. By using the filter() method, we can create a new array allOccurrences containing all elements that match the target number. You can customize the condition based on your requirements to find all occurrences ...