Undefined and Null analysis of JavaScript type system _javascript skills

Source: Internet
Author: User
Tags error handling

Front.

The General program language, which indicates null only NULL, but JavaScript designer Brendan Eich but designed a undefined, which undoubtedly increased the complexity of the program, but there are some reasons for this. This article describes in detail the undefined and null in JavaScript

Historical reasons

When JavaScript was born in 1995, it was originally like Java, setting only null as a value that represented "none." According to the C language tradition, NULL is designed to be automatically converted to 0

But Brendan Eich, a JavaScript designer, feels it's not enough, for two reasons. First, NULL is treated as an object, as in Java. However, the value of JavaScript is divided into two broad categories, the original type and the object type, and Brendan eich that the value representing "None" is best not an object. Second, the original version of JavaScript did not include the error handling mechanism, when the data type mismatch occurred, often the automatic conversion type or silently failed. Brendan Eich feel that if NULL automatically to 0, it is not easy to find errors

Therefore, Brendan Eich also designed a undefined. He distinguishes between: null is an object that represents "none", and 0;undefined is a value that represents "none" when converted to a value of Nan

However, at present null and undefined are basically synonymous, are primitive types, and have only a few subtle differences

Undefined

The undefined type has only one value, which is undefined. When a declared variable is uninitialized, the default value of the variable is undefined. So generally, undefined indicates that the variable is not initialized

var test;//undefined
console.log (test = = undefined);//true

A variable that has not yet been declared can only do one operation, using the TypeOf operator to detect its data type, but in strict mode results in an error

typeof (test);//undefined

"Show Scene"

' 1 ' declared an unassigned variable

"2" gets properties that do not exist for the object

Result of "3" function with no return value

parameter of ' 4 ' function is 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

"Type Conversions"

Boolean (undefined): false number
(undefined): NaN

Null

A null type has only one value, or null. Null is a keyword in the JavaScript language that represents a special value that is commonly used to describe the "null value"

Logically, a null value represents an empty object pointer

[Note that the]null is a null object pointer and [] is an empty array, {} is a null object, and three are not the same

 
 

Although null and undefined are different, they all represent "blank values", NULL indicates "null", and undefined represents "undefined." They are often interchangeable. Judge equality operator = = think the two are equal

 
 

In fact, because undefined and null are not constructor types, they do not have any properties or methods, and the members or methods that use. and [] to access both values will produce a type error

"Type Conversions"

Boolean (NULL): false number
(NULL): 0
String (null): ' NULL '

Here's a description of the difference between null and undefined in JavaScript.

Undefined represents a value when a variable is declared but uninitialized, and Null indicates that the object is ready to be saved, and the value of the objects is not actually saved. From a logical point of view, a null value represents an empty object pointer.

There are 5 basic types of JavaScript (ECMAScript Standard): Undefined, Null, Boolean, number, String, and a complex type object. You can see that null and undefined are different types, and uninitialized values are detected by typeof as "undefined" (strings), while null values are detected by TypeOf as "Object" (String).

It is not recommended to explicitly set a variable to undefined at any time, but it should be set to NULL if the object's variables are not actually saved.
In fact, the undefined value is derived from a null value, and the ECMAScript standard stipulates that the test for equality between the two returns true, that is,

alert (null==undefined); True
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.