DEV Community


Discussion on: Why does indexOf output like this?

trolley33 profile image
Reece Trolley • Edited

Second argument to indexOf is the starting offset. Negative offsets start from the end of the array. Also if an element is not found -1 is returned. So if you start at the last element (-1th?) There is only 41 so 16 is not an element hence returning -1. If you start from the second to last element your search array is [16, 41] which does have 16 so it outputs the position in the overall array of that element (i.e. 6th position).