JavaScript Basics _ Simple comparison undefined and null values _ basics

Source: Internet
Author: User

There are two special values in JavaScript: undefined and null, which need to be noticed when comparing them. The undefined value is obtained when reading an unassigned variable or attempting to read an attribute that is not in an object.

<! DOCTYPE html>
 
 

Output results:

 
 

Javascript also defines a special value of NULL, which is slightly different from undefined. The latter is a value that is not defined as worth, and the former is used to indicate that a value has been assigned but that the value is not a valid object, string, number, or Boolean value (that is, the definition is a value-free [no value]).

The following code uses undefined and null successively to show its different effects:

<! DOCTYPE html>
 
 

Output results:

var:undefined
prop:false

var:sunny
prop:true

var:null
prop:true

1. Check whether the variable or property is undefined or null

If you want to check whether a property is null or undefined (whichever), use the IF statement and the logical non-operator (!) only. Can.

<! DOCTYPE html>
 
 

Output results:

The name is not null or undefined the city is

null or undefined

weather is null or undefined

2. Distinguishing between null and undefined

When comparing two values, the method used should be determined as necessary. If you want to treat undefined values and null values equally, you should use the equality operator (= =) to have Javascript type cast. A variable with a value of undefined is considered equal to a variable with a value of NULL. If you want to differentiate between null and undefined, you should use the equivalent operator (= =).

<! DOCTYPE html>
 
 

Output results:

Equality:true
Identity:false

The above JavaScript basics _ simple comparison undefined and null value is small series to share all the content, hope to give you a reference, but also hope that we 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.