An analysis of undefined types in JavaScript and null types _ Basics

Source: Internet
Author: User

Undefined
indicates that the variable has been declared but has not been initialized. It should be noted that when the TypeOf operator is used to determine the data type, the values returned by the undeclared and uninitialized variables are undefined

var message;
Console.log (typeof message),//undefined
Console.log (typeof age);//undefined

Null: Represents an empty object pointer
the value returned using the typeof operator is ' object ', and it should be noted that the undefined value is derived from a null value, so ECMA-262 the equality test for them to return true.

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

The difference between undefined and null
(1) undefined means that a variable is not declared or declared but not assigned
(2) NULL is a value that represents "No value"
(3) JavaScript sets the unassigned variable default value to undefined
(4) JavaScript never sets a variable to null. It is used to show the programmer that a variable declared with VAR has no value.
(5) Undefined is not a valid JSON, and NULL is
(6) The type of undefined (typeof) is the type of Undefined,null (typeof) is object.

PS: They are all basic types
They are all Falsy (Boolean (undefined)//False, Boolean (NULL)/False)
You can judge whether a variable is undefined

 typeof variable = = "undefined"

You can tell whether a variable is null or not

 variable = = NULL

They are equal when they are compared, but they are not equal when compared with the three equals

 NULL = = undefined//true
 NULL = = undefined//False

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.