javascript Type System undefined and null Full Understanding

  • 2021-07-04 17:41:09
  • 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 Reason When JavaScript was born in 1995, it was initially set as a value for "none", just like Java1. According to the tradition of C language, null was designed to automatically convert to 0

However, the designer of JavaScript, Brendan, Eich, felt that this was not enough for two reasons. First, null is treated as an object like 1 in Java. However, the values of JavaScript are divided into two categories: original 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

The undefinedUndefined 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

Only 1 operation can be performed on variables that have not been declared, and its data type is detected by typeof operator, but an error will be caused 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, which are different


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'

Related articles: