Null and Undefind differences in JavaScript

Source: Internet
Author: User

Text of the public number

There are 5 basic types of javascript: boolean,number,null,undefined,string; and a complex type: object;

Undefined: There is only one value, and the special undefined. When declaring a variable with VAR but not initializing it, the value of the variable is undefined, in short, undefined is the value that represents the variable declared but not initialized.

Note: Values that have not been declared directly alert their value to an error instead of displaying undefined; but if a variable that declares an unassigned is the same as the typeof result shown by an undeclared variable--->undefined

code example:

<script type= "Text/javascript" >var  Str;alert (typeof str); // undefined alert (typeof message); // undefined</script>

Undefined must be nan when participating in numerical operation, and the undefined is displayed directly when the concatenation of strings is added.

code example:

<script type= "Text/javascript" >var  B;alert (b+3); // Output NAN Alert (b+ ' 3 '); // Output UNDEFINED3</script>

Null: The object is ready to be saved, and the value of the object is not actually saved. From a logical point of view, a null value represents an empty object pointer. This means that null is relative to the object, so typeof (NULL) is an object.

Null is automatically converted to 0 when participating in a numeric calculation, and string concatenation is displayed as a string "null"

code example:

<script type= "Text/javascript" >var A=null;alert (null+3+5);//8alert (null+ ' hellow ');//nullhellowalert (null+3 + ' Hellow ');//3hellowalert (null+ ' hellow ' +3);//nullhellow3</script>

  

Contact: alert (null=undefined);//true ECMAScript that undefined is derived from null, they are defined as equal.

How to differentiate?

Alert (typeofnulltypeof  undefined); alert (null = = = undefined);

Null and Undefind differences in JavaScript

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.