JavaScript Basics: How to check for null & undefined

To check if a variable(a) has the value null, use the following code:

if (a === null) // Best way to do it!

if (a == null) // This does return true if a is null but it returns true also if a is undefined... so avoid it

To check if a variable(a) has the value undefined, use the following code:

if (typeof a === "undefined") // This works

if (a === undefined) // This works too

if (a == undefined) // This does return true if a is undefined but it returns true also if a is null... so avoid it