var arr = [-1,0,1,2,3,4,5,6,7,8]; console.log(arr.indexOf(9)); //why is -1 the output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

var arr = [-1,0,1,2,3,4,5,6,7,8]; console.log(arr.indexOf(9)); //why is -1 the output

I expect -1 to be at index 0 and index of 9 to be undefined (or doesn't exist) , but javascript seems to be saying otherwise🙄! Somebody explain? https://code.sololearn.com/WOwWp0H3u7Oe/?ref=app

23rd Nov 2019, 6:15 PM
HERBERT MUHIIRWA🇺🇬
HERBERT MUHIIRWA🇺🇬 - avatar
2 Answers
+ 6
HERBERT MUHIIRWA🇺🇬 , indexOf method returns the index of a element in array. When the array doesn't contain the element it returns " -1" . There is no element 9 in the array. Hope you understand it better now🐱
23rd Nov 2019, 6:27 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
TheWh¡teCat 🇧🇬 , now I do get it clearly, and also that the -1 it returns has nothing to do with the -1 element in the array..thanks big
24th Nov 2019, 9:37 PM
HERBERT MUHIIRWA🇺🇬
HERBERT MUHIIRWA🇺🇬 - avatar