简体   繁体   中英

Why is my indexOf not showing any value other than -1?

I have a really basic array that shows variations of my name.

This is it right here..

var allTogether = ["Kenny", "Ken", "Ken", "kenny", "ken", "ken", "Kenny", "Ken", "Ken", "kenny", "ken", "ken", "Kenny", "Ken", "Ken"];

However, I tried a basic indexOf method for the array and this is the result.

allTogether.indexOf(4);
-1

Then I tried.

allTogether.indexOf('kenny');
-1

What's going on? I know that -1 means that the element has not been found but I'm missing some crucial information about this method entire.

Thanks in advance, guys. Really appreciate the feedback :)

Edit: Tried this thing out on jsfiddle and well.. it works. I can't honestly figure out why it doesn't work in chrome developer. Your guesses are as good as mine guys. That said, I do honestly appreciate the feedback. This community is grade A'! :D

allTogether.indexOf(4); returns -1 because 4 doesn't exist in your array.

allTogether.indexOf('kenny'); should return 3.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf?v=example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM