I know, I know there must be some threads covering this topic. But I used the search and didn't get the answer which fits my needs. So here we go:
How do I check a variable if it's
null
orundefined
and what is the difference between thenull
andundefined
?What is the difference between "==" and "===" (it's hard to search Google for
===
)?
The difference is subtle.
In JavaScript an
undefined
variable is a variable that as never been declared, or never assigned a value. Let's say you declarevar a;
for instance, thena
will beundefined
, because it was never assigned any value.But if you then assign
a = null;
thena
will now benull
. In JavaScriptnull
is an object (trytypeof null
in a JavaScript console if you don't believe me), which means that null is a value (in fact evenundefined
is a value).Example:
This can prove useful in function arguments. You may want to have a default value, but consider null to be acceptable. In which case you may do:
If you omit the
optional
parameterdoSomething(1, 2) then
optional will be the"three"
string but if you passdoSomething(1, 2, null)
then optional will benull
.As for the equal
==
and strictly equal===
comparators, the first one is weakly type, while strictly equal also checks for the type of values. That means that0 == "0"
will return true; while0 === "0"
will return false, because a number is not a string.You may use those operators to check between
undefined
annull
. For example:The last case is interesting, because it allows you to check if a variable is either undefined or null and nothing else:
Is the variable
null
:...but note the latter will also be true if
a
isundefined
.Is it
undefined
:...but again, note that the last one is vague; it will also be true if
a
isnull
.Now, despite the above, the usual way to check for those is to use the fact that they're falsey:
This is defined by ToBoolean in the spec.
They're both values usually used to indicate the absence of something.
undefined
is the more generic one, used as the default value of variables until they're assigned some other value, as the value of function arguments that weren't provided when the function was called, and as the value you get when you ask an object for a property it doesn't have. But it can also be explicitly used in all of those situations. (There's a difference between an object not having a property, and having the property with the valueundefined
; there's a difference between calling a function with the valueundefined
for an argument, and leaving that argument off entirely.)null
is slightly more specific thanundefined
: It's a blank object reference. JavaScript is loosely typed, of course, but not all of the things JavaScript interacts with are loosely typed. If an API like the DOM in browsers needs an object reference that's blank, we usenull
, notundefined
. And similarly, the DOM'sgetElementById
operation returns an object reference — either a valid one (if it found the DOM element), ornull
(if it didn't).Interestingly (or not), they're their own types. Which is to say,
null
is the only value in the Null type, andundefined
is the only value in the Undefined type.The only difference between them is that
==
will do type coercion to try to get the values to match, and===
won't. So for instance"1" == 1
is true, because"1"
coerces to1
. But"1" === 1
is false, because the types don't match. ("1" !== 1
is true.) The first (real) step of===
is "Are the types of the operands the same?" and if the answer is "no", the result isfalse
. If the types are the same, it does exactly what==
does.Type coercion uses quite complex rules and can have surprising results (for instance,
"" == 0
is true).More in the spec:
==
, also called "loose" equality)===
)The spec is the place to go for full answers to these questions. Here's a summary:
x
, you can:null
by direct comparison using===
. Example:x === null
undefined
by either of two basic methods: direct comparison withundefined
ortypeof
. For various reasons, I prefertypeof x === "undefined"
.null
andundefined
by using==
and relying on the slightly arcane type coercion rules that meanx == null
does exactly what you want.==
and===
is that if the operands are of different types,===
will always returnfalse
while==
will convert one or both operands into the same type using rules that lead to some slightly unintuitive behaviour. If the operands are of the same type (e.g. both are strings, such as in thetypeof
comparison above),==
and===
will behave exactly the same.More reading:
undefined
It means the variable is not yet intialized .
Example :
equals(==)
It only check value is equals not datatype .
Example :
Because it checks only value .
Strict Equals(===)
Checks the value and datatype should be same .
Example :
Because it checks the datatype x is a primitive type and y is a boolean object .
If your (logical) check is for a negation (!) and you want to capture both JS
null
andundefined
(as different Browsers will give you different results) you would use the less restrictive comparison: e.g.:This will capture both
null
andundefined
just check if a variable has a valid value like this :
it will return true if variable does't contain :