Includes - indexOf relationship

Hello all, I just learnt indexOf mechanic through courses and I passed Iterate Through All an Array’s Items Using For Loops with using includes instead.

I just want to learn are these situations mean always the same thing:

myArray.includes(elem) == false


myArray.indexOf(elem) == -1

Thanks :smile:

includes return a Boolean, depending if the thing is included or not

indexOf returns the item position, or -1 if not included

You can use both in this case, but it depends on situation

1 Like

The .includes method is newer than .indexOf, first appearing in ES2015 (ES6). I recommend using .includes these days rather than checking whether .indexOf returns -1

1 Like