Home > Article > Web Front-end > Detailed explanation of the understanding of undefined and null in JavaScript
Below I will bring you a comprehensive understanding of the JavaScript type system - undefined and null. Let me share it with you now and give it as a reference for everyone.
The previous words
General programming languages only express null, but Brendan Eich, the designer of JavaScript, designed an undefined. This undoubtedly increases the complexity of the program, but there are certain reasons for this. This article will introduce in detail undefined and null in JavaScript
Historical reasons When JavaScript was born in 1995, initially like Java, only null was set as a value representing "none". According to the tradition of the C language, null is designed to be automatically converted to 0
. However, JavaScript designer Brendan Eich feels that this is not enough for two reasons. First, null is treated as an object just like in Java. However, JavaScript values are divided into two categories: primitive types and object types. Brendan Eich feels that the value representing "none" is best not an object. Secondly, the initial version of JavaScript did not include an error handling mechanism. When a data type mismatch occurs, the type is often automatically converted or fails silently. Brendan Eich felt that if null automatically converted to 0, it would be difficult to find errors
Therefore, Brendan Eich designed another undefined. He distinguishes it like this: null is an object that represents "none" and is 0 when converted to a numerical value; undefined is a primitive value that represents "none" and is NaN when converted to a numerical value. However, currently null They are basically synonymous with undefined. They are both primitive types with only some subtle differences.
undefinedThe Undefined type has only one value, which is undefined. When a declared variable is not initialized, the variable's default value is undefined. So generally, undefined means that the variable has not been initialized
var test;//undefined console.log(test == undefined);//true var test = undefined;//undefined
Only one operation can be performed for a variable that has not been declared.
Use the typeof operator to detect its data type, but it will cause an error in strict modetypeof(test);//undefined
【1】Declared unassigned variable
【2】Get the non-existent attribute of the object
【3】The execution result of a function with no return value
【4】The parameters of the function are 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'
The Null type has only one value, which is null. null is a keyword in the JavaScript language, which represents a special value and is often used to describe "null value"
From a logical perspective, the null value represents a null object pointer
[Note]null is 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 "vacancy of value", and null represents "empty value" ", undefined means "undefined". The two are often interchangeable. The 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. Use . and [] to access these two Any member or method with a value will generate a type error
[Type conversion]Boolean(null): false
Number(null): 0
String(null): 'null'
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
JavaScript function expression (picture and text tutorial) Javascript inheritance mechanism (detailed answer, picture Text tutorial)Javascript creates classes and objects (Graphic tutorial)The above is the detailed content of Detailed explanation of the understanding of undefined and null in JavaScript. For more information, please follow other related articles on the PHP Chinese website!