const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
// expected output: 1
// start from index 2
console.log(beasts.indexOf('bison', 2));
// expected output: 4
console.log(beasts.indexOf('giraffe'));
// expected output: -1
I've been diving into this JavaScript syntax. I stumbled upon this code snippet on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf and it talks about the fromIndex parameter being the starting point for the search. However, as I analyze the following lines of code:
// start from index 2
console.log(beasts.indexOf('bison', 2));
// expected output: 4
I noticed that Camel is at index 2. But how does the counting lead to an expected output of 4?
Just experimenting by changing the second parameter:
// start from index 3
console.log(beasts.indexOf('bison', 3));
With Duck appearing at index 3, I still get an output of 4 for indexOf('bison', 3).
How exactly does this counting work?