Difference between JavaScript undefined and null

expound

Most computer languages ​​have one and only one value representing "none", for example, NULL in C language, null in Java language, None in Python language, and nil in Ruby language.

It's a little strange that the JavaScript language actually has two values ​​for "none": undefined and null. why is that?

1. Similarity

In JavaScript, assigning a variable to undefined or null, honestly, makes little difference.

var a = undefined;

var a = null;

In the above code, the a variable is assigned as undefined and null respectively, which are almost equivalent.

Both undefined and null are automatically converted to false in the if statement, and the equality operator even directly reports that the two are equal.

if (!undefined) 
    console.log('undefined is false');
// undefined is false

if (!null) 
    console.log('null is false');
// null is false

undefined == null
// true

The above code shows how similar the behavior of the two is!

Since the meaning and usage of undefined and null are similar, why set two such values ​​at the same time? Isn't this unreasonably increasing the complexity of JavaScript and confusing beginners?

The Dart language, a substitute for the JavaScript language developed by Google, clearly stipulates that there is only null and no undefined!

2. Historical reasons

Recently, I stumbled upon the answer to this question while reading my new book "Speaking JavaScript"!

Turns out, this has something to do with the history of JavaScript.
When JavaScript was born in 1995, it initially set null as the value for "none", just like Java.

According to the C language tradition, null is designed to be automatically converted to 0.

Number(null)
// 0

5 + null
// 5

However, Brendan Eich, the designer of JavaScript, felt that this was not enough for two reasons.

First, null is treated as an object just like in Java.
However, JavaScript data types are divided into primitive types (primitive) and composite types (complex), and Brendan Eich thinks that the value of "nothing" is best not an object.

Second, the original versions of JavaScript did not include error handling mechanisms, and when data type mismatches occurred, types were often converted automatically or failed silently. Brendan Eich feels that if null is automatically turned to 0, it is not easy to find errors.

Therefore, Brendan Eich designed another undefined.

3. Initial design

The original version of JavaScript differentiated like this:
null is an object representing "nothing", which is 0 when converted to a number;
undefined is a primitive value representing "nothing", which is NaN when converted to a number.

Number(undefined)
// NaN

5 + undefined
// NaN

4. Current usage

However, such a distinction was quickly shown to be infeasible in practice. Currently, null and undefined are basically synonymous, with some minor differences.

null means "no object", i.e. there should be no value there. Typical usage is:

(1) As a parameter of a function, it means that 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", i.e. there should be a value here, but it hasn't been defined. Typical usage is:

(1) When a variable is declared, but not assigned a value, it is equal to undefined.

(2) When calling the function, the parameter that should be provided is not provided, and the parameter is equal to undefined.

(3) The object has no assigned property, and the value of the property is undefined.

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

var i;
i // undefined

function f(x){
    
    console.log(x)}
f() // undefined

var  o = new Object();
o.p // undefined

var x = f();
x // undefined

Guess you like

Origin blog.csdn.net/weiguang102/article/details/124097882