JS judge null null and string null shorthand method

Source: Internet
Author: User
Tags null null

  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.    

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.