Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAbe Layee
8,378 PointsindexOf() please explain it to me.
I am confused about the indexOf() method where the teacher used < - in his code. When indexOf() can't find a value in an array, it returns a negative 1. Example
var items = ['Hat', 'Ball', 'Shoes'];
console.log(itmes.indexOf('lime') > -1); // I am totally lost with the negative one part. Why would this be useful?
3 Answers
Jeremy Fisk
7,768 PointsJavaScript will return -1 if the user calls the indexOf()
method on an array and does not find the item searched for (i.e the argument passed into indexOf()
), in your example lime
is the argument and it is not found in the array so the value of -1 is returned.
If the item is in the array, indexOf()
will return the position location of the item searched for. So, if you called items.indexOf('Hat')
the method will return 0, which is the location of 'Hat'
in items
This is useful, in the event you developed a program that needed to search an array to quickly determine if an email address or some other item was present. If you called the indexOf
method and it returned a -1, you could then move on in your program knowing that your search efforts did not result in the item you were searching for...
Daniel Rosensweig
5,074 PointsAbraham, I think you're assuming you already know whether the item you're looking for is in your array. IndexOf tells you where an item is in your array, if it's there. Many times, you already know it's in your array, and you just want to know where to access it. But it's also useful that it lets you know when the item is not in your array at all.
If you're looking for an item in your array, presumably you're going to do something with that item next. Maybe you'll pass that index to another function, or there could be a whole list of things you'd want done with the item. The -1 response tells you that you don't need to do all of those things, because there is no such item. It may save you performance, because you're not wastefully processing things for this nonexistent item, or it may even prevent something from crashing later on.
In the case of a nonexistent item, Javascript couldn't just pass you a number that IS in your array, where you wouldn't know the difference! It could either pass you a number that would never appear as an array index (what they actually did, of course), or it could throw an error that you'd have to catch to avoid crashing. Which would you rather get back?
Abe Layee
8,378 PointsThanks guys for clarifying this to me. I appreciate both of your answers.
Jeremy Fisk
7,768 Pointsyou bet, good luck
Daniel Rosensweig
5,074 PointsYou're welcome!