Detailed analysis of differences between js and undefined, undefined, and null Types

Source: Internet
Author: User

Detailed analysis of differences between js and undefined, undefined, and null Types

This article mainly analyzes and introduces the differences between js undefined type, undefined, null, and NaN. If you need a friend, I hope to help you.

Js determines the undefined type

 

Today, when showModalDialog is used to open the page and return values. When you click the close button on the page or click Close in the browser, the returned value is undefined.

Therefore, smart judgment

 

Var reValue = window. showModalDialog ("","","");

If (reValue = undefined ){

Alert ("undefined ");

}

 

Unable to judge, found the information to use typeof

 

Method:

If (typeof (reValue) = "undefined "){

Alert ("undefined ");

}

Typeof returns a string, which may be "number", "string", "boolean", "object", "function", or "undefined"

 

 

Differences between undefined, null, and NaN in js

 

1. Type Analysis:

The data types in js include undefined, boolean, number, string, and object. The first four are original data types and the second are reference data types.

Var a1;

Var a2 = true;

Var a3 = 1;

Var a4 = "Hello ";

Var a5 = new Object ();

Var a6 = null;

Var a7 = NaN;

Var a8 = undefined;

Alert (typeof a); // display "undefined"

Alert (typeof a1); // display "undefined"

Alert (typeof a2); // displays "boolean"

Alert (typeof a3); // display "number"

Alert (typeof a4); // display "string"

Alert (typeof a5); // displays "object"

Alert (typeof a6); // displays "object"

Alert (typeof a7); // display "number"

Alert (typeof a8); // display "undefined"

 

From the code above, we can see that undefined is undefined for undefined values and definitions. null is a special object and NaN is a special number.

 

2. Comparison

Var a1; // The a1 value is undefined.

Var a2 = null;

Var a3 = NaN;

Alert (a1 = a2); // display "true"

Alert (a1! = A2); // display "false"

Alert (a1 = a3); // display "false"

Alert (a1! = A3); // display "true"

Alert (a2 = a3); // display "false"

Alert (a2! = A3); // display "true"

Alert (a3 = a3); // display "false"

Alert (a3! = A3); // display "true"

 

From the code above, we can draw a conclusion: (1) undefined is equal to null; (2) NaN is not equal to any value and is not equal to itself.

 

JavaScript undefined attributes

 

Definition and usage

The undefined attribute is used to store the undefined value of JavaScript.

 

Syntax

Undefined

 

Description

You cannot use the for/in loop to enumerate the undefined attribute, or use the delete operator to delete it.

Undefined is not a constant. You can set it to another value.

Undefined is returned when you try to read non-existent object attributes.

 

Tips and comments

Tip: You can only use the = operation to test whether a value is undefined, because the = operator considers the undefined value to be equivalent to null.

Note: null indicates no value, while undefined indicates an undeclared variable, a declared variable without a value, or an object attribute that does not exist.

 

Instance

In this example, we will check the undefined one of the two variables:

<Script type = "text/javascript">

Var t1 = ""

Var t2

If (t1 = undefined) {document. write ("t1 is undefined ")}

If (t2 = undefined) {document. write ("t2 is undefined ")}

</Script>

 

Output:

T2 is undefined

 

 

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.