In JavaScript, which values can be used as conditions for if?
1. Boolean Variable True/false
2, the number is not 0, not nan/(0 or Nan)
See the example below to think of negative numbers as if statements are false.
var i =-1;
if (i) {
Alert (' here ');
}else{
Alert (' Test is ok! ');
}3, objects not null/(null or undefined)
4. String not Empty strings ("")/Empty Strings ("")
To sum up, for strings, do not write a large stack of if (str!=null && str!=undefined && str!= "), just use one sentence
if (!STR) {
Do something
}
It's OK.
For non-null judgments of numbers, consider using the isNaN () function, which is not equal to any type of data, including itself, and can only be judged by isNaN (). For a numeric type, A is 0 o'clock if (a) is false in the IF (a) statement, not 0 o'clock if (a) is true
var b;
var a = 0;
A = a + B;
if (a) {
Alert (' 1 ');
}else{
Alert (' 2 ');
}
if (isNaN (a)) {
Alert (' A is NaN ');
}
This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20121025/33198.html
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.