How does it work indexof

there is an example from mdn

var indices = [];
var array = ["a", "b", "a", "c", "a", "d"];
var element = "a";
var idx = array.indexOf(element);
while (idx != -1) {
  idx = array.indexOf(element, idx + 1);

// [0, 2, 4]

Now I would like to understand how it works, I never understood it, but still, who knows?
Standard explanations don’t suit me either)))) I don’t understand them, because where do they get the values -1 and for what?

The script finds all indices of a specific element (“a”) in an array and stores them in another array (indices). Here’s how it works:

  • var indices = []; initializes an empty array to store indices.
  • var array = ["a", "b", "a", "c", "a", "d"]; is the array to search.
  • var element = "a"; is the element you’re looking for.
  • var idx = array.indexOf(element); finds the first occurrence of “a” and stores the index (0) in idx.

The while loop then:

  • Checks if idx is -1. -1 means the element was not found.
  • If not -1, adds idx to indices.
  • Searches again starting from idx + 1 using array.indexOf(element, idx + 1);

The loop repeats until idx is -1, meaning no more “a” found.

The value -1 is a standard way to indicate “not found” in many programming contexts.

1 Like

and how can idx be -1 if the report starts from 0 then you need to write that while (idx== array) that is, the element is found, a match is found, add indices to it

If the element doesn’t exist it can’t take a valid value, so it uses -1.

1 Like

I don’t understand this at all, I can only agree that it is necessary, but why and why here you can understand it as you want)It’s clear that there is no -1 index, since the report starts with 0, why write it, and even so it’s not clear

Not sure if you are aware that there is something called developer documents

And a developer should read them.

1 Like

when I don’t understand, the first thing I do is start reading the documentation, but I don’t understand 90% of what they write there, so after that I turn to the forum,

Say you have an array of grades, like this:

const grades = ['A', 'B', 'C', 'D', 'F'];

You want to find the index of grade I.

const index=grades.indexOf('I');

What would the index of grade I be? It doesn’t exist in the array, so you need to return a value that indicates it doesn’t exist. Since any positive number could be a valid array index, you can’t return any of those. Returning false/null/undefined means you’re returning a different type. You could do that, but those can be easily confused with index zero which leads to bugs, so that’s not a great choice. What’s left? Array’s cannot have negative indexes, so returning -1 works well to handle this case.

In general, these kinds of things are considered a Sentinel value


So lets try asking it this way:

If you were writing indexOf, as a developer, how would you tell the user that what they tried to find wasn’t in the array?

(Things to keep in mind: 0 is index of the first element of the array. false will implicitly convert to a 0 if used in a numeric context.)

a little bit it became clear to me, -1 says if there are no matches, then why write != -1? why not == -1?

Because the while loop would break immediately every time, it would do nothing.
It is saying continue the loop while it is not returning an invalid result.
So continue when the result works, but stop when the result is invalid.

What the LOOP is trying to do is to find ALL instances of the value in the array.
indexOf will return the first index it finds, or -1 if it finds none.
When is the loop done looping? when it’s found all of them.
How does it know when it’s found all of them? When there’s none left.

So While the indexOf is not -1, it has more work to do.

1 Like

This loop

var idx = array.indexOf(element);
while (idx != -1) {
  idx = array.indexOf(element, idx + 1);

Would translate to this, in simple language

search for element;
while (element was found){
   add index of element to list;
  search for element again (starting from the last known position);

If you changed the condition to use == instead, you’d change the loop to

search for element;
while (element was not found){
   add index of element to list;
  search for element again (starting from the last known position);

Which would either loop endlessly (because the element doesn’t exist) or would never loop (because the element does).

I just realized where the answer came from, but why such an entry idx + 1?

The + 1 moves the value up one, to the next index.
So 0 becomes 1, 1 becomes 2, etc…
In preparation for the next run of the loop.

The second parameter to indexOf tells it which index to start with when searching for the element. idx is the position of the element that was just found, so if you use that for the second parameter it is just going to return idx again since array[idx] == element.

By adding 1, the search resumes with the next index after the one that was just found.

So when you consulted the documentation about array.indexOf() what didn’t you understand about


The indexOf() method of Array instances returns the first index at which a given element can be found in the array, or -1 if it is not present.

It seems perfectly obvious to me.
In some computer languages the maximum value would be returned
Also the maximum value for a cardinal(unsigned) number is the same binary representation as -1 in a twos-compliment integer value, and that is probably why -1 was chosen for not present, or maybe not.