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 trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

Ali Amirazizi
Ali Amirazizi
16,087 Points

Why does the condition "> -1 " need to be included?

Why would we need to include that bit of code ( from "if (inStock.indexOf(search) > -1)" ) to our program. Isn't that redundant since the indexOf method returns a value of an item if it's in the array, and returns a -1 if it's not. What am I missing?

4 Answers

Hugo Paz
Hugo Paz
15,622 Points

HI Ali,

The way the if works is to check if the condition is true or not. The only falsy values in javascript are : false 0 (zero) "" (empty string) null undefined NaN (a special Number value meaning Not-a-Number!)

So if the value was in the first position, index 0, it would be false and the if condition would not run. On the other hand -1 is not false so the if condition would run. What you want is for the condition to run as long as the returning value is not -1.

This is why you if (inStock.indexOf(search) > -1) instead of if (inStock.indexOf(search)).

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hi Hugo,

shouldn't it return true if the value is at index 0, since 0 > -1?

Hugo Paz
Hugo Paz
15,622 Points

The explanation i gave was why he cannot just use inStock.indexOf(search) to verify if the condition is true or not.

0 would be a case of a false negative. The product exists but 0 is evaluated as false so the if never runs. You need the > -1

Ali Amirazizi
Ali Amirazizi
16,087 Points

Thanks Vitto! That clears things up. The condition is verifying that the item is inside the array by checking to see if it is >-1. If it is, then the item is inside the array, else it's not.

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Ali.

I don't remember the particular case, but I guess with that piece of script you want to verify if that item is inStock (an array). If it return more than -1 (>-1), you have the item in the stock, if it returns -1 the item you are searching form is not in the array.

Please note that indexOf returns the index of the value.

Ali Amirazizi
Ali Amirazizi
16,087 Points

I think I understand. The condition ">-1" is to ensure index 0 of the array does not evaluate to a falsy value. This is because the indexOf method is being used inside an If-statement, in which the conditions are being evaluated for truthy or falsy.

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

No Ali, it is not that. I think that index 0 thing made a little confusion:

The if condition you wrote is only used to verify if the value is in the array. As you wrote indexOf returns -1 if value is not in the array, so if we want to check if one value is in the array we need an indexOf that returns more than -1 (>-1). ANY value whose indexOf returns a number >-1 is inside the array.

If it returns 0 it means that the value is at the very first place, and the condition returns true.

Here you have more explanation: http://www.w3schools.com/jsref/jsref_indexof.asp