Method _ javascript tips for determining null, undefined, and NaN in JS

Source: Internet
Author: User
This article mainly introduces how to judge null, undefined, and NaN in JS. If you need it, you can refer to the following str = "s" ++;
Then Nan appears. Find it for a while.
The collected data is judged as follows:
1. Determine undefined:

The Code is as follows:


Var tmp = undefined;
If (typeof (tmp) = "undefined "){
Alert ("undefined ");
}


Note: typeof returns a string. There are six possible types: "number", "string", "boolean", "object", "function", and "undefined"
2. null judgment:

The Code is as follows:


Var tmp = null;
If (! Tmp & typeof (tmp )! = "Undefined" & tmp! = 0 ){
Alert ("null ");
}


3. Judge NaN:

The Code is as follows:


Var tmp = 0/0;
If (isNaN (tmp )){
Alert ("NaN ");
}


Note: If the result of comparing NaN with any value (including its own) is false, you cannot use the = OR = Operator to determine whether a value is NaN.
Tip: The isNaN () function is usually used to check the results of parseFloat () and parseInt () to determine whether they represent valid numbers. Of course, you can also use the isNaN () function to detect arithmetic errors, such as using 0 as the divisor.
4. Determine undefined and null:

The Code is as follows:


Var tmp = undefined;
If (tmp = undefined)
{
Alert ("null or undefined ");
}


The Code is as follows:


Var tmp = undefined;
If (tmp = null)
{
Alert ("null or undefined ");
}


Description: null = undefined

5. Determine undefined, null, and NaN:

The Code is as follows:


Var tmp = null;
If (! Tmp)
{
Alert ("null or undefined or NaN ");
}


Tip: Generally, this is enough to differentiate data.

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.