[ACCEPTED]-When is null or undefined used in JavaScript?-undefined

Accepted answer
Score: 124

I find that some of these answers are vague 21 and complicated, I find the best way to 20 figure out these things for sure is to just 19 open up the console and test it yourself.

var x;

x == null            // true
x == undefined       // true
x === null           // false
x === undefined      // true

var y = null;

y == null            // true
y == undefined       // true
y === null           // true
y === undefined      // false

typeof x             // 'undefined'
typeof y             // 'object'

var z = {abc: null};

z.abc == null        // true
z.abc == undefined   // true
z.abc === null       // true
z.abc === undefined  // false

z.xyz == null        // true
z.xyz == undefined   // true
z.xyz === null       // false
z.xyz === undefined  // true

null = 1;            // throws error: invalid left hand assignment
undefined = 1;       // works fine: this can cause some problems

So 18 this is definitely one of the more subtle 17 nuances of JavaScript. As you can see, you 16 can override the value of undefined, making it somewhat 15 unreliable compared to null. Using the == operator, you 14 can reliably use null and undefined interchangeably as 13 far as I can tell. However, because of the 12 advantage that null cannot be redefined, I might 11 would use it when using ==.

For example, variable != null will 10 ALWAYS return false if variable is equal to either 9 null or undefined, whereas variable != undefined will return false if variable is 8 equal to either null or undefined UNLESS undefined is reassigned 7 beforehand.

You can reliably use the === operator 6 to differentiate between undefined and null, if you need 5 to make sure that a value is actually undefined (rather 4 than null).

According to the ECMAScript 5 spec:

  • Both Null and Undefined are two of the six built in types.

4.3.9 undefined value

primitive value used when a variable 3 has not been assigned a value

4.3.11 null value

primitive value 2 that represents the intentional absence 1 of any object value

Score: 81

The DOM methods getElementById(), nextSibling(), childNodes[n], parentNode() and so on return 8 null (defined but having no value) when the 7 call does not return a node object.

The 6 property is defined, but the object it refers to 5 does not exist.

This is one of the few times 4 you may not want to test for equality-

if(x!==undefined) will 3 be true for a null value

but if(x!= undefined) will be true 2 (only) for values that are not either undefined or 1 null.

Score: 50

You get undefined for the various scenarios:

You 13 declare a variable with var but never set 12 it.

var foo; 
alert(foo); //undefined.

You attempt to access a property on an 11 object you've never set.

var foo = {};
alert(foo.bar); //undefined

You attempt to access 10 an argument that was never provided.

function myFunction (foo) {
  alert(foo); //undefined.
}

As cwolves 9 pointed out in a comment on another answer, functions 8 that don't return a value.

function myFunction () {
}
alert(myFunction());//undefined

A null usually 7 has to be intentionally set on a variable 6 or property (see comments for a case in 5 which it can appear without having been 4 set). In addition a null is of type object and 3 undefined is of type undefined.

I should also note 2 that null is valid in JSON but undefined 1 is not:

JSON.parse(undefined); //syntax error
JSON.parse(null); //null
Score: 9

I might be missing something, but afaik, you get undefined only

Update: Ok, I missed a lot, trying to complete:

You 16 get undefined...

... when you try to access properties 15 of an object that don't exist:

var a = {}
a.foo // undefined

... when you 14 have declared a variable but not initialized 13 it:

var a;
// a is undefined

... when you access a parameter for which 12 no value was passed:

function foo (a, b) {
    // something
}

foo(42); // b inside foo is undefined

... when a function 11 does not return a value:

function foo() {};
var a = foo(); // a is undefined

It might be that 10 some built-in functions return null on some 9 error, but if so, then it is documented. null is 8 a concrete value in JavaScript, undefined is not.


Normally 7 you don't need to distinguish between those. Depending 6 on the possible values of a variable, it 5 is sufficient to use if(variable) to test whether a 4 value is set or not (both, null and undefined evaluate 3 to false).

Also different browsers seem to be 2 returning these differently.

Please give 1 a concrete example.

Score: 3

Regarding this topic the specification (ecma-262) is 7 quite clear

I found it really useful and 6 straightforward, so that I share it: - Here 5 you will find Equality algorithm - Here you will find Strict equality algorithm

I bumped 4 into it reading "Abstract equality, strict 3 equality, and same value" from mozilla 2 developer site, section sameness.

I hope 1 you find it useful.

Score: 0

A property, when it has no definition, is 5 undefined. null is an object. It's type 4 is null. undefined is not an object, its 3 type is undefined.

This is a good article 2 explaining the difference and also giving 1 some examples.

null vs undefined

More Related questions