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, 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.
The undefinedUndefined 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'
The above article about javascript type system - undefined and null is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.