JavaScript type system--undefined and NULL

Source: Internet
Author: User

xTable of Contents [1] reason [2]undefined [3]null Front 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); // true var 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);  // undefined  var  o = {};console.log (O.P);  // undefined  function   F () {};console.log (f ());  // undefined  function  f (x) { X;} Console.log (f ());  // undefined  console.log ( void  (0)); // undefined  

"Type Conversion"

false Number (undefined):  Nanstring (undefined): ' undefined '    

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

[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 (typeofnull); // ' object '

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 ' ‘

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.