Differences between null, NaN, and undefined in Javascript _ javascript tips-js tutorial

Source: Internet
Author: User
The data types in js include undefined, boolean, number, string, object, and so on. The first four are the original data types and the first 5th are the reference data types. Next we will share with you the differences between them. 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.
Code

The Code is as follows:


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

The Code is as follows:


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
<The value is equivalent to the undefined operator that the operation is used to test whether a value is undefined, because = "=">
<Indicates no value, but>

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

The Code is as follows:


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.