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 trialSoufiane Ez Zine
Full Stack JavaScript Techdegree Graduate 17,754 Pointswhats the difference between inStock.indexOf(search) < -1 and inStock.indexOf(search) !== -1 ?
as the title says, I tried the program with if ( inStock.indexOf(search) !== -1 ) and if ( inStock.indexOf(search) < -1 ) and the reslt seems to be the same. my question is why to opt for one rather than the other.
thank you in advance ^^
2 Answers
Steven Parker
231,236 PointsI'm surprised the difference wasn't noticeable. The value of "indexOf" should never be less than -1, so the test for "< -1
" should never be true.
On the other hand, if an index is found, the method will return the index number of the match, which will be a non-negative integer. So testing for "!== -1
" should be true anytime a match is found.
For future use, the "includes" method can do the same job as the latter but without the comparison. For example: "inStock.includes(search)
". The result is true when a match is found.
Soufiane Ez Zine
Full Stack JavaScript Techdegree Graduate 17,754 PointsI understand thank you very much ^^
Christopher Evans
9,896 PointsChristopher Evans
9,896 Pointswhat about the "find" method?
I tried "inStock.find(search)" but had no success...
Steven Parker
231,236 PointsSteven Parker
231,236 PointsThe "find" method syntax requires a callback function (instead of a string) as the argument.