I have seen a piece of code in the web.
function isMobile() {
var index = navigator.appVersion.indexOf("Mobile");
return (index > -1);
}
Why can’t we just check if index is > or < 0? Please explain returning index > -1.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the result of
indexOfis-1, the string was not found.0would indicate that is is in the string, and that it is, in fact, at the start of the string.Therefore, if you tested for
> 0, then you’d be checking if it existed somewhere not at the start of the string.!(index < 0)andindex >= 0would both work, too, but the former is a little more complex than it needs to be.If you were confused about the syntax, returning the result of a comparison is the same as returning
trueif the comparison results intrueand returningfalsewhen the comparison results infalse.