Null

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.

Tags:

Comments

Related Articles