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 Array indexOf() Method - how to test for item present or not

I have seen that most developers use the greater than comparison operator to test for the presence of an item in an array, for example:

myArray.indexOf(search) > -1;

I have not seen an example of somebody using the not equal operator, such as:

myArray.indexOf(search) != -1;

Is there a problem using the second statement?

2 Answers

Steven Parker
Steven Parker
243,318 Points

No problem, these are functionally equivalent.

Developers tend to be as concise as possible, and the inequality operator takes an additional character.

Thanks! I knew there must be an answer, that's what i wanted to know...