JavaScript type system--undefined and null comprehensive understanding of the basics

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 for the 1995 JavaScript was born, initially like Java, set only null as a value that represents "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

The undefinedundefined 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
var test = undefined;//undefined

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

console.log (void (0));//undefined

"Type Conversions"

Boolean (undefined): false number
(undefined): NaN
String (undefined): ' Undefined '  

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

Console.log (typeof null);//' object '

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

Console.log (null = = undefined);//true

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 '

The above JavaScript type system--undefined and null comprehensive understanding is small series share to everybody's content, hope can give everybody a reference, also hope everybody support cloud habitat community.

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.