Original: http://www.jb51.net/article/47234.htm
The recent discovery of the JavaScript code that I wrote was rather bloated, so I began to study the shorthand method of JavaScript. This way, we can make our JavaScript code look fresh and improve our technology. So how do you abbreviate the judgment?
The following is a shorthand method for judging null.
The code is as follows:
ifnullundefined‘‘) { var variable2 = variable1; }
The above means that if variable1 is not an empty object, or undefined, or not equal to an empty string, declare a variable2 variable and assign variable1 to Variable2. That is, if variable1 exists, then the value of Variable1 is assigned to Variable2, or an empty string if it does not exist. such as the following shorthand code.
Shorthand code:
The code is as follows:
var‘‘;
The following is an incorrect method:
The code is as follows:
varnull; ifnull) { alert("is null"); }
When exp is undefined, it also gets the same result as NULL, although null and undefined are not the same. Note: This method can be used to determine both null and undefined.
The code is as follows:
varnull; if (!exp) { alert("is null"); }
If exp is undefined, or the number zero, or false, it will also get the same result as NULL, although NULL is not the same as the two. Note: This method can be used to determine both null, undefined, number 0, and false.
The code is as follows:
varnull; if (typeof"null") { alert("is null"); }
In order to be backwards compatible, exp is null, typeof null always returns object, so this cannot be judged.
The code is as follows:
varnull; if (isNull(exp)) { alert("is null"); }
Determines whether a string is empty
s matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [FNRTV]. In many cases, the length is used to directly determine if the string is empty, as follows:
The code is as follows:
var‘‘; if0) { alert(‘不能为空‘); }
But what if the user entered a space, a tab, a page break? This is also not empty, but such data is not what we want it.
In fact, you can use regular expressions to remove these "empty" symbols to judge
The code is as follows:
var strings = ' ; if (Strings.replace (/(^s*) | ( s*$)/g , "" ). Length ==0 ) {alert ( ' cannot be empty ' ); }
s lowercase s yes, matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [FNRTV].
Judge is empty how shorthand, for everyone introduced here, hope the above method can help everyone.
?
JS to determine null null and string null shorthand method