We know that by default, the ‘obj’ below is string. Without using ‘parseInt’, how does JavaScript compare it with a number?
obj = document.frm.weight.value;
if( obj < 0 || obj > 5 ){
alert("Enter valid range!");
return false;
}
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 one of the operands of
<or>is number, the other one will be casted to a number.EDIT and further explanation:
If it is not possible to cast the other parameter into a number, it is casted into the special value called
NaN– the abbreviation stands for “Not a Number”. TheNaNvalue has a special property that it is absolutely incomparable – all relation operators like<,>and=will returnfalseif one of the arguments isNaN.Also, note that in the second line, if we used
it would alert
true, becauseparseIntreads"3.5"from the string"3.5?", then stops reading at"?"and thus evaluates to3.5. However,returns
falsebecause the cast fromstringtonumberis not as benevolent asparseInt. As"3.5"indeed isn’t a number, it is casted toNaN.