This question already has an answer here:
I am really confused as to when JavaScript returns null
or undefined
. Also different browsers seem to be returning these differently.
Could you please give some examples of null
/undefined
with the browsers that return them.
While I am now clear on the undefined
aspect, I am still not 100% clear on null
. Is it similar to a blank value?
E.g. You have a text box which does not have any value set. Now when you try to access its value, will it be null
or undefined
and are they similar?
You get undefined for the various scenarios:
You declare a variable with var but never set it.
You attempt to access a property on an object you've never set.
You attempt to access an argument that was never provided.
As cwolves pointed out in a comment on another answer, functions that don't return a value.
A null usually has to be intentionally set on a variable or property (see comments for a case in which it can appear without having been set). In addition a null is of type
object
and undefined is of typeundefined
.I should also note that null is valid in JSON but undefined is not:
A property, when it has no definition, is undefined. null is an object. It's type is null. undefined is not an object, its type is undefined.
This is a good article explaining the difference and also giving some examples.
null vs undefined
The DOM methods
getElementById()
,nextSibling()
,childNodes[n]
,parentNode()
and so on returnnull
(defined but having no value) when the call does not return a node object.The property is defined, but the object it refers to does not exist.
This is one of the few times you may not want to test for equality-
if(x!==undefined)
will be true for a null valuebut
if(x!= undefined)
will be true (only) for values that are not eitherundefined
ornull
.Regarding this topic the specification (ecma-262) is quite clear
I found it really useful and straightforward, so that I share it: - Here you will find Equality algorithm - Here you will find Strict equality algorithm
I bumped into it reading "Abstract equality, strict equality, and same value" from mozilla developer site, section sameness.
I hope you find it useful.
I might be missing something, but afaik, you getundefined
onlyUpdate: Ok, I missed a lot, trying to complete:
You get
undefined
...... when you try to access properties of an object that don't exist:
... when you have declared a variable but not initialized it:
... when you access a parameter for which no value was passed:
... when a function does not return a value:
It might be that some built-in functions return
null
on some error, but if so, then it is documented.null
is a concrete value in JavaScript,undefined
is not.Normally you don't need to distinguish between those. Depending on the possible values of a variable, it is sufficient to use
if(variable)
to test whether a value is set or not (both,null
andundefined
evaluate tofalse
).Please give a concrete example.
I find that some of these answers are vague and complicated, I find the best way to figure out these things for sure is to just open up the console and test it yourself.
So this is definitely one of the more subtle nuances of JavaScript. As you can see, you can override the value of
undefined
, making it somewhat unreliable compared tonull
. Using the==
operator, you can reliably usenull
andundefined
interchangeably as far as I can tell. However, because of the advantage thatnull
cannot be redefined, I might would use it when using==
.For example,
variable != null
will ALWAYS return false ifvariable
is equal to eithernull
orundefined
, whereasvariable != undefined
will return false ifvariable
is equal to eithernull
orundefined
UNLESSundefined
is reassigned beforehand.You can reliably use the
===
operator to differentiate betweenundefined
andnull
, if you need to make sure that a value is actuallyundefined
(rather thannull
).According to the ECMAScript 5 spec:
Null
andUndefined
are two of the six built in types.4.3.9 undefined value
4.3.11 null value