JavaScript type system--undefined and NULL

Source: Internet
Author: User

Previous words

General programming language, which means null only, but JavaScript designer Brendan Eich design a undefined, which undoubtedly increases the complexity of the program, but there are some reasons for this. This article will detail the undefined and null in JavaScript

Historical reasons

When JavaScript was born in 1995, initially, like Java, only null was set as the value representing "none". According to the traditional C language, NULL is designed to automatically convert to 0.

However, JavaScript designers Brendan Eich that this is not enough, there are two reasons. First, NULL is treated as an object, as in Java. However, the value of JavaScript is divided into two categories: primitive type and object type, Brendan eich think that the value of "none" is best not an object. Second, the initial version of JavaScript does not include the error handling mechanism, when data type mismatch occurs, is often the type of automatic conversion or silently fail. Brendan Eich feel that if NULL is automatically converted to 0, it is not easy to find an error

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

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

Undefined

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

var test;//undefinedconsole.log (test = = undefined);//truevar test = undefined;//undefined

You can only perform one operation on a variable that has not been declared, use the TypeOf operator to detect its data type, but strict mode causes an error

typeof (test);//undefined

"Scene appears"

"1" declared unassigned variable

"2" gets the property that the object does not exist

"3" execution result of a function with no return value

The parameters of the "4" function are not passed in

"5" void (expression)

var i;console.log (i);//undefinedvar o = {};console.log (O.P);//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 '    

"Type identification"

Identify the undefined type and use the TypeOf operator to

Console.log (typeof undefined);//' Undefined ' console.log (typeof ' undefined ');//' String '

[note] Since undefined is not a keyword, it will be overridden in the Ie8-browser, and will be overridden in the high-version function scope, so you can replace the undefined with void.

var undefined = 10;console.log (undefined),//ie8-browser is 10, and the browser under the high version is undefined
function T () {    var undefined = ten;    Console.log (undefined);} Console.log (t ());//all browsers are 10

Null

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

At a logical angle, a null value represents an empty object pointer

Console.log (document.getElementById (' Test '));//null

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

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

Different objects are represented as binary at the bottom, and in JavaScript, the first three bits of the binary are 0 and are judged to be of type object, and the null binary representation is all 0, so the typeof is executed when the ' object ' is returned.

Although null and undefined are different, they all represent "empty values", null means "null", and undefined means "undefined". The two are often interchangeable. Judging equality operators = = think they 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, and the members or methods that use. and [] to access both values will produce a type error

"Type Conversion"

Boolean (NULL): Falsenumber (NULL):  0String (NULL): ' NULL '

"Type identification"

To identify a null type, use the typeof operator to be non-nullable because the operator returns ' object ' and null is considered null object pointer

The best way to determine whether a value is a null type is to make an identical comparison directly and null

Console.log (typeof null);//' object ' console.log (null = = null);//trueconsole.log (undefined = = = null);// Falseconsole.log (' null ' = = = null);//false

Resources

"1" Ruan one peak JavaScript standard reference Tutorial--Grammar Overview Http://javascript.ruanyifeng.com/grammar/basic.html#toc21
"2" W3school-javascript Advanced Tutorial--Original type http://www.w3school.com.cn/js/pro_js_primitivetypes.asp
"3" JavaScript Definitive Guide (6th edition) 3rd Chapter type, value, and variable
"4" JavaScript Advanced Programming (3rd Edition), chapter 3rd Basic Concepts

JavaScript type system--undefined and NULL

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.