Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the understanding of undefined and null in JavaScript

亚连
Release: 2018-05-19 16:37:17
Original
1811 people have browsed it

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
Copy after login

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 mode

typeof(test);//undefined
Copy after login

【Occurrence scenario】

 【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
Copy after login

[Type conversion]

Boolean(undefined):  false
Number(undefined):  NaN
String(undefined):  'undefined'
Copy after login

null

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'
Copy after login

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
Copy after login

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'
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!