dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Client Scripting
Expertise: Beginner
Jul 8, 1997

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


undefined vs. null

Question:
 

Answer:
You are probably familiar with a special value in JavaScript called null. This is a value you can assign to a variable when you want to indicate that the variable has no particular value.

Well, kind-of. The fact is, null is a value, too -- just like true, false, NaN, and Infinity.

Contrary to popular belief, unassigned variables (and object properties) do not begin with a default value of null. Instead, they begin with a special value called undefined.

Although it's difficult to distinguish between undefined and null, the two values are, in fact, distinct, as demonstrated in the following scripts.

script 1

value = window . wiper; alert ("value is " + value);

script 2

value = null; alert ("value is " + value);

script 3

value = window . wiper; alert (value == null);

script 4

value = null; alert (value == null);

In Script 1, value equals undefined because window does not have a property named wiper. In Script 2, however, value is defined: It is defined as null. Script 3 demonstrates that when undefined is compared to null, the result is true. But null also equals null, as shown in Script 4.

So what does all this mean? Fortunately, not much: In most cases, there is little need to distinguish between null and undefined. However, if you have a habit of comparing variables to null just to see if they are defined, don't forget that the results may be inconclusive. A true comparison means either that the variable is undefined or that the variable has been defined as null.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date