Js determines that the instance is Null and the string is Null.

Source: Internet
Author: User

In javascript, what are the differences between Null and Null strings? Let me introduce how to judge the differences between Null and Null strings.

Recently, I suddenly found that my JavaScript code is bloated, so I began to study the simplified JavaScript method. In this way, we can make our JavaScript code look fresh and improve our technology. So how can I make it empty?


The following is a shorthand Method for null determination.

The Code is as follows: Copy code

If (variable1! = Null | variable1! = Undefined | variable1! = ''){
Var variable2 = variable1;
}

The above means that if variable1 is not an empty object, or is not defined, or is not equal to a null string, declare a variable2 variable and assign variable1 to variable2. That is to say, if variable1 exists, the value of variable1 is assigned to variable2. If not, it is a null string. The abbreviated code is as follows.


Abbreviated code:

The Code is as follows: Copy code


Var variable2 = variable1 | '';


The following are incorrect methods:

The Code is as follows: Copy code
Var exp = null;
If (exp = null)
{
Alert ("is null ");
}

When exp is undefined, it will also get the same result as null, although null and undefined are different. Note: This method can be used to determine both null and undefined.

The Code is as follows: Copy code

Var exp = null;
If (! Exp)
{
Alert ("is null ");
}

If exp is undefined, zero number, or false, the same result is obtained as null, although null is different from the two. Note: This method can be used to determine null, undefined, zero, or false at the same time.

The Code is as follows: Copy code

Var exp = null;
If (typeof exp = "null ")
{
Alert ("is null ");
}

For backward compatibility, if exp is null, typeof null always returns the object, so this judgment cannot be made.

The Code is as follows: Copy code

Var exp = null;
If (isNull (exp ))
{
Alert ("is null ");
}


Determines whether the string is null.

S matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [fnrtv]. In many cases, length is used to directly judge whether the string is null, as shown below:

The Code is as follows: Copy code
Var strings = '';
If (string. length = 0)
{
Alert ('cannot be blank ');
}

But what if the user inputs spaces, tabs, and page breaks? In this case, it is not empty, but such data is not what we want.

 

In fact, you can use a regular expression to remove these "null" symbols for determination.

The Code is as follows: Copy code

Var strings = '';
If (strings. replace (/(^ s *) | (s * $)/g, ""). length = 0)
{
Alert ('cannot be blank ');
}
 

S in lower case, s is used to match any blank characters, including spaces, tabs, and page breaks. It is equivalent to [fnrtv].

 

Let's take a look at the shorthand for null. I hope the above method will be helpful to you. The above is my opinion. If you have any questions, contact us. We are willing to study with you.

Related Article

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.