351

I’m a bit confused about JavaScript’s undefined and null values.

What does if (!testvar) actually do? Does it test for undefined and null or just undefined?

Once a variable is defined can I clear it back to undefined (therefore deleting the variable)?

Can I pass undefined as a parameter? E.g.:

function test(var1, var2, var3) {

}

test("value1", undefined, "value2");
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
AJ.
  • 10,732
  • 13
  • 41
  • 50

10 Answers10

579

I'm a bit confused about Javascript undefined & null.

null generally behaves similarly to other scripting languages' concepts of the out-of-band ‘null’, ‘nil’ or ‘None’ objects.

undefined, on the other hand, is a weird JavaScript quirk. It's a singleton object that represents out-of-band values, essentially a second similar-but-different null. It comes up:

  1. When you call a function with fewer arguments than the arguments list in the function statement lists, the unpassed arguments are set to undefined. You can test for that with eg.:

    function dosomething(arg1, arg2) {
        if (arg2===undefined)
        arg2= DEFAULT_VALUE_FOR_ARG2;
        ...
    }
    

    With this method you can't tell the difference between dosomething(1) and dosomething(1, undefined); arg2 will be the same value in both. If you need to tell the difference you can look at arguments.length, but doing optional arguments like that isn't generally very readable.

  2. When a function has no return value;, it returns undefined. There's generally no need to use such a return result.

  3. When you declare a variable by having a var a statement in a block, but haven't yet assigned a value to it, it is undefined. Again, you shouldn't really ever need to rely on that.

  4. The spooky typeof operator returns 'undefined' when its operand is a simple variable that does not exist, instead of throwing an error as would normally happen if you tried to refer to it. (You can also give it a simple variable wrapped in parentheses, but not a full expression involving a non-existant variable.) Not much use for that, either.

  5. This is the controversial one. When you access a property of an object which doesn't exist, you don't immediately get an error like in every other language. Instead you get an undefined object. (And then when you try to use that undefined object later on in the script it'll go wrong in a weird way that's much more difficult to track down than if JavaScript had just thrown an error straight away.)

    This is often used to check for the existence of properties:

    if (o.prop!==undefined) // or often as truthiness test, if (o.prop)
       ...do something...
    

    However, because you can assign undefined like any other value:

    o.prop= undefined;
    

    that doesn't actually detect whether the property is there reliably. Better to use the in operator, which wasn't in the original Netscape version of JavaScript, but is available everywhere now:

    if ('prop' in o)
        ...
    

In summary, undefined is a JavaScript-specific mess, which confuses everyone. Apart from optional function arguments, where JS has no other more elegant mechanism, undefined should be avoided. It should never have been part of the language; null would have worked just fine for (2) and (3), and (4) is a misfeature that only exists because in the beginning JavaScript had no exceptions.

what does if (!testvar) actually do? Does it test for undefined and null or just undefined?

Such a ‘truthiness’ test checks against false, undefined, null, 0, NaN and empty strings. But in this case, yes, it is really undefined it is concerned with. IMO, it should be more explicit about that and say if (testvar!==undefined).

once a variable is defined can I clear it back to undefined (therefore deleting the variable).

You can certainly assign undefined to it, but that won't delete the variable. Only the delete object.property operator really removes things.

delete is really meant for properties rather than variables as such. Browsers will let you get away with straight delete variable, but it's not a good idea and won't work in ECMAScript Fifth Edition's strict mode. If you want to free up a reference to something so it can be garbage-collected, it would be more usual to say variable= null.

can I pass undefined as a parameter?

Yes.

Navid Khan
  • 979
  • 11
  • 24
bobince
  • 528,062
  • 107
  • 651
  • 834
  • 12
    What a fantastic answer. I've just failed the JavaScript test: http://perfectionkills.com/javascript-quiz/ Later I'm going to re-read your answer and try the test again! – Skilldrick Feb 10 '10 at 16:09
  • SO's kangax, there, with the scary JS-obsession. :-) I didn't get them all either! Reminded me of one more case that results in `undefined` though — added. – bobince Feb 11 '10 at 02:39
  • A slight error: (4) The spooky typeof operator returns "undefined" rather than `undefined`. And I always use `typeof` to check for undefined variables: `undefined` can be reassigned, since it's just a property of the global object. Also, the `in` operator only made it into IE in version 5.5, but I don't suppose that's going to affect many people now. – Tim Down Apr 13 '10 at 16:02
  • 1
    fixed, ta. (I tend not to worry too much about reassignment, as there are so many things a script could redefine to mess everything up that the general case is impossible to solve.) – bobince Apr 13 '10 at 16:44
  • 2
    @bobince `undefined` is a singleton object? What do you mean? It's a primitive value, and not an object. – Šime Vidas Nov 23 '10 at 19:08
  • Why no mention of testing for undefined using typeof? if (typeof foo == "undefined") ? – Hortitude Aug 10 '11 at 13:28
  • 2
    @Hortitude: That's point (4). I generally wouldn't `typeof foo=='undefined'`; it tends to be used for (4a) global-sniffing, in which case I'd prefer to be explicit and say `'foo' in window`, or (4b) testing against the `undefined`-value itself, in which case I'd prefer to be readable and say `foo===undefined`. In theory testing `typeof` against `'undefined'` could have a use case that other constructs couldn't provide: sniffing for the existence of local variables. However in reality you pretty much always know what variables are declared local. – bobince Aug 10 '11 at 19:44
  • 4
    One caveat with #2, usually a function with no `return` statement returns `undefined`, but if the function is a constructor (invoked with the `new` operator) it will return the new object (the value of `this` inside the constructor) despite not having a `return` statement. `console.log((function (){}()));` returns `undefined`. `console.log((new function (){}()));` returns an object. – Useless Code Apr 17 '13 at 22:16
  • @bobince marvelous! one quick glance and i know what i need to know. – Prynz Jan 29 '14 at 09:28
  • @bobince I know this is a total aside, but what definition of "out-of-band" are you applying to characterize `null` and `undefined`? – Hans Sep 20 '14 at 20:49
  • You deserve a gold medal and the title "Saviour of Mankind" for this answer! – evilReiko Dec 07 '16 at 07:30
25

You cannot (should not?) define anything as undefined, as the variable would no longer be undefined – you just defined it to something.

You cannot (should not?) pass undefined to a function. If you want to pass an empty value, use null instead.

The statement if(!testvar) checks for boolean true/false values, this particular one tests whether testvar evaluates to false. By definition, null and undefined shouldn't be evaluated neither as true or false, but JavaScript evaluates null as false, and gives an error if you try to evaluate an undefined variable.

To properly test for undefined or null, use these:

if(typeof(testvar) === "undefined") { ... }

if(testvar === null) { ... }
Dustin Getz
  • 21,282
  • 15
  • 82
  • 131
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
  • 1
    The triple-equals checks for type as well, so no type coercion is carried out. Douglas Crockford advises against using the type-coercing operators (`==` and `!=`). – Skilldrick Feb 10 '10 at 09:30
  • 4
    You can define something as undefined. `var a= undefined`. You can pass `fn(undefined)`. If you want to find the difference between an undefined property `p` of an object `o`, and a property `p` that has defined and set to `undefined`, you have to use the `'p' in o` operator. – bobince Feb 10 '10 at 10:23
  • 1
    Is there a reason why one should not test for `testvar === undefined` rather than the more complicated `typeof(testvar) === "undefined"`? – ddaa Feb 10 '10 at 10:25
  • In general, `typeof` tests are intended to support cross-frame scripting: that is to say, each document object has its own copy of the basic datatypes, so `s instanceof String` will fail when `s` is from a different document. However, I believe `undefined` is, as an exception, the same object from document to document, so it may not be necessary in this case. – bobince Feb 10 '10 at 11:03
  • 4
    There is another good reason for using a `typeof` test for undefined variables: unlike `null`, `undefined` is simply a property of the global object and may itself be redefined. It only takes a particular coding style and a missing equals sign and `undefined` is silently changed: `if (undefined = someVar) {...}` – Tim Down Apr 13 '10 at 15:48
  • bobince: `s instanceof String` isn't a great example, since strings have the primitive/object confusion (`"blah" instanceof String` returns `false`, for example). `arr instanceof Array` is a more likely example of how you could get stung by an object coming from a different document. – Tim Down Apr 13 '10 at 15:51
  • @TimDown - I don't think this small fact is mentioned anything like enough here. Because of the quirk that it is possible to override the 'undefined' object (if I can call it that) IMHO you shouldn't ever check for == undefined or === undefined - but only ever use the typeof check. With that in mind I'm not sure the best way to SET something to undefined (in the few cases where null isn't good enough and you really need to do this). – Ian Grainger Sep 03 '13 at 11:06
  • 2
    @IanGrainger: ECMAScript 5 (implemented in current versions of all browsers) mostly fixes this by making `undefined` an immutable property of the global object, so it's safer than it used to be. However, it is still possible to define a mutable variable called `undefined` within a function, so the problem hasn't entirely gone away. – Tim Down Sep 03 '13 at 11:51
  • 1
    If you say something is undefined, aren't you *defining* it? – OdraEncoded Aug 16 '14 at 12:20
19

The basic difference is that undefined and null represent different concepts.

If only null was available, you would not be able to determine whether null was set intentionally as the value or whether the value has not been set yet unless you used cumbersome error catching: eg

var a;

a == null; // This is true
a == undefined; // This is true;
a === undefined; // This is true;

However, if you intentionally set the value to null, strict equality with undefined fails, thereby allowing you to differentiate between null and undefined values:

var b = null;
b == null; // This is true
b == undefined; // This is true;
b === undefined; // This is false;

Check out the reference here instead of relying on people dismissively saying junk like "In summary, undefined is a JavaScript-specific mess, which confuses everyone". Just because you are confused, it does not mean that it is a mess.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined

This behaviour is also not specific to JavaScript and it completes the generalised concept that a boolean result can be true, false, unknown (null), no value (undefined), or something went wrong (error).

http://en.wikipedia.org/wiki/Undefined_value

Todd
  • 877
  • 8
  • 5
  • And still `null` would've worked just fine. `a == null` is `true` because Javascript type coercion. – Rukshan Jan 24 '20 at 07:40
14

The best way to check for a null values is

if ( testVar !== null )
{
    // do action here
}

and for undefined

if ( testVar !== undefined )
{
    // do action here
}

You can assign a avariable with undefined.

testVar = undefined;
//typeof(testVar) will be equal to undefined.
rahul
  • 184,426
  • 49
  • 232
  • 263
8

YES, you can, because undefined is defined as undefined.

console.log(
   /*global.*/undefined === window['undefined'] &&
   /*global.*/undefined === (function(){})() &&
   window['undefined']  === (function(){})()
) //true

your case:

test("value1", undefined, "value2")

you can also create your own undefined variable:

Object.defineProperty(this, 'u', {value : undefined});
console.log(u); //undefined
7

To answer your first question, the not operator (!) will coerce whatever it is given into a boolean value. So null, 0, false, NaN and "" (empty string) will all appear false.

Skilldrick
  • 69,215
  • 34
  • 177
  • 229
  • And also empty string and NaN (Not a number) – MBO Feb 10 '10 at 09:24
  • `undefined` will not appear as `false`, it will throw a 'is not defined' error. – Tatu Ulmanen Feb 10 '10 at 09:28
  • 1
    Tatu Ulmanen: not true. `if (undefined) {/* Do stuff*/}` will not give an error, since `undefined` exists as a property of the global object. What will give an error is something like `if (someUndeclaredVariable) {/* Do stuff*/}` – Tim Down Apr 13 '10 at 15:54
4

JavaScript, how to set a variable to undefined on commandline:

Set a variable to undefined in the js javascript command line terminal that comes with Java on Ubuntu 12.10.

el@defiant ~ $ js

js> typeof boo
"undefined"

js> boo
typein:2: ReferenceError: boo is not defined

js> boo=5
5

js> typeof boo
"number"

js> delete(boo)
true

js> typeof boo
"undefined"

js> boo
typein:7: ReferenceError: boo is not defined

If you set a variable to undefined in a javascript:

Put this in myjs.html:

<html>
<body>
    <script type="text/JavaScript">
        document.write("aliens: " + aliens);
        document.write("typeof aliens: " + (typeof aliens));
        var aliens = "scramble the nimitz";
        document.write("found some aliens: " + (typeof aliens));
        document.write("not sayings its aliens but... " + aliens);
        aliens = undefined;
        document.write("aliens deleted");
        document.write("typeof aliens: " + (typeof aliens));
        document.write("you sure they are gone? " + aliens);
    </script>
</body>
</html>

It prints this:

aliens: undefined
typeof aliens: undefined
found some aliens: string
not sayings its aliens but... scramble the nimitz
aliens deleted
typeof aliens: undefined
you sure they are gone? undefined

WARNING! When setting your variable to undefined you are setting your variable to another variable. If some sneaky person runs undefined = 'rm -rf /'; then whenever you set your variable to undefined, you will receive that value.

You may be wondering how I can output the undefined value aliens at the start and have it still run. It's because of javascript hoisting: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
3

Try this:

// found on UglifyJS
variable = void 0;
vvahans
  • 1,849
  • 21
  • 29
Cqq Cqq
  • 31
  • 1
2

The for if (something) and if (!something) is commonly used to check if something is defined or not defined. For example:

if (document.getElementById)

The identifier is converted to a boolean value, so undefined is interpreted as false. There are of course other values (like 0 and '') that also are interpreted as false, but either the identifier should not reasonably have such a value or you are happy with treating such a value the same as undefined.

Javascript has a delete operator that can be used to delete a member of an object. Depending on the scope of a variable (i.e. if it's global or not) you can delete it to make it undefined.

There is no undefined keyword that you can use as an undefined literal. You can omit parameters in a function call to make them undefined, but that can only be used by sending less paramters to the function, you can't omit a parameter in the middle.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

Just for fun, here's a fairly safe way to assign "unassigned" to a variable. For this to have a collision would require someone to have added to the prototype for Object with exactly the same name as the randomly generated string. I'm sure the random string generator could be improved, but I just took one from this question: Generate random string/characters in JavaScript

This works by creating a new object and trying to access a property on it with a randomly generated name, which we are assuming wont exist and will hence have the value of undefined.

function GenerateRandomString() {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for (var i = 0; i < 50; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

var myVar = {}[GenerateRandomString()];
Community
  • 1
  • 1
rdans
  • 2,179
  • 22
  • 32