recently started to study the shorthand method for JavaScript, and here's a short method for judging null
Recently I found myself writing JavaScript code is bloated, so I began to study the shorthand method of JavaScript. This will make our JavaScript code look refreshing and also improve our technology. So what's the shorthand for a null judgment?? The following is a shorthand method for determining null. Code as follows: if (variable1!== null | | variable1!== undefined | | variable1!== ") { var variable2 = variable 1; } 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 the variable1 exists then the value of the variable1 is assigned to the Variable2, or an empty string if it does not exist. Like the following shorthand code. Shorthand Code: code is as follows: var variable2 = Variable1 | | '; Below is the incorrect method: code is as follows copy code code as follows: var exp = null; if (exp = null) { alert ("is nul L "); } exp is undefined, the same result can be obtained with NULL, although null and undefined are different. Note: This method can be used to determine both null and undefined. Code as follows: var exp = null; if (!exp) { alert (' Is null '); } If exp is undefined, or number zero, or FALSE, you 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. Code as follows: var exp = null; if (typeof exp = = "null") { ALert ("is null"); } for backward compatibility, when EXP is null, typeof null always returns object, so this cannot be judged. Code as follows: var exp = null; if (isNull (exp)) { alert ("is null"); } To determine whether the string is empty &N Bsp 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 whether the string is empty, as follows: code as follows: var strings = '; if (string.length = = 0) { alert (' Cannot is empty '); } But what if the user enters a space, tab, or page break? This is not empty, but the data is not what we want. can actually use regular expressions to remove these "empty" symbols to determine the code as follows: var strings = '; if (strings.replace (^s*) | ( s*$)/g, ""). Length ==0) { alert (' cannot be empty '); } s lowercase s is to match any white space characters, including spaces, tabs, page breaks, and so on. equivalent to [FNRTV]. Judgment is empty how to abbreviate, for everyone to introduce here, I hope the above methods can help.