Analysis of undefined and null in Javascript Type System

  • 2021-07-04 17:38:16
  • OfStack

Previous words

1-like programming language, only null is empty, but Brendan Eich, the designer of javascript, has designed an undefined, which undoubtedly increases the complexity of the program, but there is also a certain reason for doing so. This article will introduce undefined and null in javascript in detail

Historical reasons

When JavaScript was born in 1995, like Java1, only null was set as the value indicating "none". According to the tradition of C language, null was designed to automatically convert to 0

However, Brendan Eich, the designer of JavaScript, felt that this was not enough for two reasons. First of all, null is treated as an object like 1 in Java. However, the values of JavaScript are divided into two categories: primitive type and object type. Brendan Eich thinks that the value indicating "none" is preferably not an object. Secondly, the original version of JavaScript does not include error handling mechanism, and when data type mismatch occurs, it often automatically converts the type or silently fails. Brendan Eich thinks that if null automatically converts to 0, it is not easy to find errors.

Therefore, Brendan Eich has designed another undefined. He makes the distinction as follows: null is an object that means "nothing" and when converted to a numeric value it is 0; undefined is an original value that means "nothing" and when converted to a numeric value it is NaN

However, at present, null and undefined are basically synonymous, both of which are primitive types with only one slight difference

undefined

The Undefined type has only one value, which is undefined. When the declared variable is uninitialized, the default value of the variable is undefined. So generally, undefined means that the variable is not initialized


var test;//undefined
console.log(test == undefined);//true
var test = undefined;//undefined 

You can only perform 1 operation on variables that have not been declared, and use the typeof operator to detect their data type, but it will cause an error in strict mode

typeof(test);//undefined

[Scene Appears]

'1' Declared unassigned variable

'2' Gets the property of the object that does not exist

'3' Execution result of function with no return value

Arguments to the '4' function were not passed in

"5" void (expression)


var i;
console.log(i);//undefined
var o = {};
console.log(o.p);//undefined
function f(){};
console.log(f());//undefined
function f(x){return x;}
console.log(f());//undefined
console.log(void(0));//undefined 

"Type Conversion"


Boolean(undefined):    false
Number(undefined):    NaN
String(undefined):        'undefined' 

null

The Null type has only one value, which is null. null is the key word of javascript language, which represents a special value and is often used to describe "null value"

From a logical point of view, the null value represents a null object pointer

[Note] null is an empty object pointer, while [] is an empty array and {} is an empty object


console.log(typeof null);//'object' 

Although null and undefined are different, they both mean "value vacancy", null means "null value", and undefined means "undefined". The two are often interchangeable. The judgment equality operator = = considers the two to be equal


console.log(null == undefined);//true 

In fact, because undefined and null are not constructor types, they do not have any properties and methods, and members or methods that use. and [] to access these values will produce a type error

"Type Conversion"


Boolean(null):     false
Number(null):     0
String(null):      'null'

Let's introduce the difference between null and undefined in javascript

undefined denotes the value of the variable when it is declared but not initialized, and null denotes the value of the object that is prepared to be saved but has not actually been saved. From a logical point of view, the null value represents a null object pointer.

There are five basic types in JavaScript (ECMAScript standard): Undefined, Null, Boolean, Number, String, and one complex type Object. It can be seen that null and undefined belong to different types, and the uninitialized defined value is detected as "undefined" (string) by typeof, while the null value is detected as "object" (string) by typeof.

It is not recommended to explicitly set a variable to undefined at any time, but if the variable that holds the object does not actually hold the object, it should be set to null.
In fact, the undefined value is derived from the null value, and the ECMAScript standard specifies that the true should be returned by testing the equality of two, that is


alert(null==undefined); // true

Related articles: