The connection and difference between JavaScript Null, Undefined, and Nan

Source: Internet
Author: User
Tags function prototype

I believe many beginner JavaScript will have this idea: why JavaScript is null and undefined, relative to C, C + +, Java, they are all just null, why JavaScript is different

One, historical reasons

The web search learned that,when JavaScript was born in 1995, initially, like Java, only null was set as the value representing "none". According to the traditional C language, NULL is designed to automatically convert to 0.

However, JavaScript designers Brendan Eich that this is not enough, there are two reasons.

First, NULL is treated as an object, as in Java. However, the data type of JavaScript is divided into two categories: primitive type (primitive) and composition type (complex), Brendan Eich think that the value of "none" is best not an object.

Second, the initial version of JavaScript does not include the error handling mechanism, when data type mismatch occurs, is often the type of automatic conversion or silently fail. Brendan Eich feels that if NULL is automatically converted to 0, it is not easy to find an error.

Therefore, Brendan Eich also designed a undefined.

Second, similarity

In reality, we use null and undefined to find that both have strong similarities.

1 var NULL ; 2 var a = undefined;

The above two variables are assigned, to be honest, basically they are not much different.

Let's look at the following code:

var NULL  ; var b =//  true

In operator operations, the two are equivalent, in fact, null and undefined will default to false in the IF statement judgment, so why set a undefined if both meanings and usages are so similar? Don't worry, let's go on to see a piece of code:

1 var NULL  ; 2 var b = undefined; 3 console.log (typeof(a)); 4 console.log (typeof(b)); 5 //    Object6//   undefined

From the code can be seen, a B assignment null,undefined, through the type to know that a is an object, b or undefined;

There are only a few minor differences between null and undefined at present:

null means "no object", typical usage is:

  (1)    for function parameters, which means that the function parameter is an empty object,  (2) as the end of the  function prototype chain;

object.getprototypeof (Object.prototype) // NULL

Undefined represents a "missing value", and the typical usage is:

(1) The variable is declared, but when it is not assigned, it is equal to undefined.

(2) when calling a function, the argument that should be supplied is not provided, which equals undefined.

(3) The object does not have an assigned property, and the value of this property is undefined.

(4) When the function does not return a value, undefined is returned by default.

1 vari;2I//undefined3 4 functionf (x) {Console.log (x)}5F ()//undefined6 7 varo =NewObject ();8O.p//undefined9 Ten varx =f (); OneX//undefined

After reading the null undefined, let's take a look at NaN, or look at the code first:

1 var NULL  ; 2 var b = undefined; 3 var c = NaN; 4 Console.log (a==c);   5 Console.log (b==c); 6 console.log (typeof(c)); //  false //  false//   number

See from the code that NaN is neither null nor undefined, but a number, a special number, why is it a special number?

1 var c = NaN; 2 var d= "Hello World"; 3 Console.log (IsNaN (d)); 4 console.log (c==NaN); true //False

From the above, Nan is not equal to itself, then it is used to express what, the word is that the Nan attribute is used to refer to a special non-numeric value, so NaN is often judged as a non-numeric type,

You cannot use the for/in loop to enumerate the NaN property, nor can you delete it by using the delete operator. NaN is not a constant, you can set it to another value .

The connection and difference between JavaScript Null, Undefined, and Nan

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.