null, undefined, NaN in JavaScript

Source: Internet
Author: User


1. Judge whether each other is equal

function Btnclick () {alert (null = = NULL);//true alert (undefined==undefined);//true alert (Nan==nan);//false}

Summarize:

(1) Two null is equal

(2) Two undefined are equal

(3) Two Nan is unequal

function Btnclick () {alert (null = = undefined),//true alert (null = = = undefined),//false alert (null = = NaN);//false alert (undefined = = NaN);//false}

Summarize:

(1) null and undefined are equal by using "= =", while "= = =" is not equal

(2) null and Nan are not equal

(3) undefined and Nan are not equal

function Btnclick () {alert (typeof (null));//object alert (typeof (undefined));//undefined}

Summarize:

(1) The result of typeof (null) is "Object"

(2) The result of typeof (undefined) is "undefined"



2. Determine if null, undefined, NaN


Use = =, = = =, isxxx, typeof, Instanceof


2.1. Judge whether it is undefined

the right approach :

var tmp = Undefined;function Btnclick () {if (typeof (tmp) = = = "undefined") {alert ("TMP is undefined!"); }}

Or

var tmp = NULL;    function Btnclick () {if (tmp = = = undefined) {alert ("TMP is undefined!"); }}


the wrong approach :

var tmp = undefined;    Null,undefinedfunction Btnclick () {if (tmp = = undefined) {alert ("TMP is undefined!"); }}

Analysis: If you use tmp==undefined, the value of TMP will be determined to be true regardless of whether it is null or undefined.


2.2. Determine if NULL

the right approach :

var tmp = NULL; Null,nan,undefinedfunction Btnclick () {if (!tmp && typeof (TMP)! = "undefined" &&!isnan (tmp)) {A    Lert ("TMP is null!"); }}

Or

var tmp = NULL; Null,nan,undefinedfunction Btnclick () {if (Tmp==null && typeof (TMP)! = "undefined") {alert ("TMP is null!    "); }}


2.3. Determine if it is Nan

The right approach:

var tmp = NaN; Null,nan,undefinedfunction Btnclick () {if (IsNaN (TMP) && typeof (TMP)! = "undefined") {alert ("TMP is nan!    "); }}


2.4. Determine if null or undefined

The right approach:

var tmp = NULL;    Null,nan,undefinedfunction Btnclick () {if (TMP = = null) {alert ("TMP is null or undefined!"); }}

Or

var tmp = undefined;    Null,nan,undefinedfunction Btnclick () {if (tmp = = undefined) {alert ("TMP is null or undefined!"); }}


2.5. Judge whether it is undefined or Nan

Correct practice:

var tmp = NaN;    Null,nan,undefinedfunction Btnclick () {if (IsNaN (TMP)) {alert ("TMP is NaN or undefined!"); }}


2.6. Determine if null or undefined or Nan

The right approach:

var tmp = NULL;    Null,nan,undefinedfunction Btnclick () {if (!tmp) {alert ("TMP is null or undefined or nan!"); }}










null, undefined, NaN in JavaScript

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.