The difference between undefined and null in JS, which comes with the history of producing undefined

Source: Internet
Author: User

Most computer languages have and have only one value that represents "none", for example, the Null,java language of the C language, the Null,python language of the None,ruby language of nil.

Oddly enough, the JavaScript language actually has two values that represent "none": Undefined and null. Why is this?

First, similarity

In JavaScript, a variable is assigned a value of undefined or null, to be honest, almost indistinguishable.

var a = undefined;var a = null;

In the above code, the A variable is assigned a value of undefined and null, which is almost equal to each other.

Undefined and null are automatically converted to false in the IF statement, and the equality operator even reports that they are equal.

If(!undefined. Log ( ' undefined is false ' ; Undefined is falseif  ( ! null) Console.log ( ' null is False ' " ; Null is falseundefined == null //true             

The code above shows how similar the two behaviors are!

Since the meaning of undefined and null is similar to that of usage, why should you set two such values at the same time, which is not an unwarranted increase in the complexity of JavaScript, so that beginners are troubled? The dart language, an alternative to the JavaScript language developed by Google, explicitly stipulates that only null, no undefined!

Second, historical reasons

Recently, when I was reading a new book, "Speaking JavaScript", I accidentally found the answer to this question!

Originally, this is related to the history of JavaScript. 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.

Number(null)// 05 + null// 5

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.

Third, the original design

The original version of JavaScript is distinguished by the following: null is an object that represents "none", and a value of 0;undefined is a primitive value that represents "none" and Nan when converted to a numeric value.

Number(undefined)// NaN5 + undefined// NaN
Iv. Current Usage

However, the above distinction, in practice, is quickly proved to be not OK. At present, null and undefined are basically synonymous, with only a few subtle differences.

Null means "No object", that is, there should be no value at all. Typical uses are:

(1) As a function parameter, the parameter of the function is not an object.

(2) As the end point of the object prototype chain.

Object.getPrototypeOf(Object.prototype)// null

Undefined means "missing value", that is, there should be a value here, but there is no definition. Typical uses are:

(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.

var i; IUndefinedfunctionF(x){Console.log (X}f ( //Undefinedvar o = new object (;o . P//Undefinedvar x = f () ;x//undefined         

The difference between undefined and null in

JS, with the history of generating undefined

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.