Hostinicon
GET HOSTING FROM $3.95/MO PLUS A FREE YEAR ON TUTS+ (RRP $180). HURRY OFFER LIMITED. Check it out
Advertisement

Null

by
Gift

Get a free year on Tuts+ this month when you purchase a Siteground hosting plan from $3.95/mo

This post is part of a series called JavaScript Succinctly.
Working With Primitive String, Number and Boolean Values
Undefined

You can use null to explicitly indicate that an object property does not contain a value. Typically, if a property is set up to contain a value, but the value is not available for some reason, the value null should be used to indicate that the reference property has an empty value.

Sample: sample60.html

Don't confuse null with undefined. undefined is used by JavaScript to tell you that something is missing. null is provided so you can determine when a value is expected but not available yet.


typeof returns null values as "object"

For a variable that has a value of null, the typeof operator returns "object. If you need to verify a null value, the ideal solution would be to see if the value you are after is equal to null. In the following sample, we use the === operator to specifically verify that we are dealing with a null value.

Sample: sample61.html


Conclusion

When verifying a null value, always use === because == does not distinguish between null and undefined.

Advertisement