The previous words
In general programming languages, only null means empty, but JavaScript designer Brendan Eich designed an undefined, which undoubtedly increases the complexity of the program, but there is 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, it was initially set like Java, only null was set as a value indicating "none". According to the tradition of C language, null is designed to automatically turn to 0
However, JavaScript designer Brendan Eich thinks that this is not enough, for two reasons. First, null is treated as an object like in Java. However, JavaScript values are divided into two categories: primitive types and object types. Brendan Eich thinks that the value that means "none" is best not an object. Secondly, the initial version of JavaScript did not include an error handling mechanism. When data type mismatch occurs, it often automatically converts the type or fails silently. Brendan Eich thinks that if null automatically turns to 0, it is difficult to find errors
Therefore, Brendan Eich designed another undefined. It distinguishes it like this: null is an object that represents "none", which is 0 when converted to a numeric value; undefined is an original value that represents "none", which is NaN when converted to a numeric value
However, currently null and undefined are basically synonymous, both are primitive types, and there are only some slight differences.
undefined
The Undefined type has only one value, which is undefined. When the declared variable is not initialized, the default value of the variable is undefined. So generally, undefined means that the variable is not initialized
var test;//undefinedconsole.log(test == undefined);//truevar test = undefined;//undefined
For variables that have not been declared, only one operation can be performed, using the typeof operator to detect their data types, but it will cause an error in strict mode
typeof(test);//undefined
【Scene Appearance】
【1】Unvalued variables declared
【2】Get the attributes that do not exist in the object
【3】Execution result of function without return value
【4】The function parameters are not passed in
【5】void(expression)
var i;console.log(i);//undefinedvar o = {};console.log(op);//undefinedfunction f(){};console.log(f());//undefinedfunction f(x){return x;}console.log(f());//undefinedconsole.log(void(0));//undefined【Type conversion】
Boolean(undefined): falseNumber(undefined): NaNString(undefined): 'undefined'
null
There is only one value for the Null type, which is null. null is a keyword in the javascript language. It represents a special value and is often used to describe "null value".
Logically, the null value represents an empty object pointer
[Note] null is an empty object pointer, while [] is an empty array, {} is an empty object, the three are different
console.log(typeof null);//'object'
Although null and undefined are different, they both represent "empty of value", null represents "null value", and undefined represents "undefined". The two are often interchangeable. Judge equality operator == think that the two are equal
console.log(null == undefined);//true
In fact, because undefined and null are not constructor types, they do not have any properties and methods. Using . and [] to access members or methods of both values will produce a type error.
【Type conversion】
Boolean(null): falseNumber(null): 0String(null): 'null'
Below I will introduce the difference between null and undefined in javascript
undefined represents the value when the variable is declared but not initialized, and null represents the value that is ready to be used to save the object, but has not really saved the object. From a logical point of view, the null value represents an empty object pointer.
There are 5 basic types in JavaScript (ECMAScript standard): Undefined, Null, Boolean, Number, String, and a complex type Object. It can be seen that null and undefined belong to different types. The value uninitialized is detected by typeof as "undefined" (string), while the null value is detected by typeof as "object" (string).
It is not recommended to explicitly set a variable to undefined at any time, but if the variable that saves the object has not really saved the object, it should be set to null.
In fact, the undefined value is derived from a null value. The ECMAScript standard stipulates that the equality test of the two must be returned true, that is,
alert(null==undefined); // true