whats 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 ^^
Steven Parker182,603 Points
I'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.
I understand thank you very much ^^