Categories
javascript jquery null undefined variables

How can I determine if a variable is ‘undefined’ or ‘null’?

2506

How do I determine if variable is undefined or null?

My code is as follows:

var EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
  // DO SOMETHING
};
<div id="esd-names">
  <div id="name"></div>
</div>

But if I do this, the JavaScript interpreter halts execution.

4

3355

You can use the qualities of the abstract equality operator to do this:

if (variable == null){
    // your code here.
}

Because null == undefined is true, the above code will catch both null and undefined.

13

  • 8

    I’m trying to test if event is null in firefox and an error blocks the execution: “event is undefined”

    Sep 6, 2018 at 10:32


  • 5

    @MichaelFever How does that not work? Copy paste this in your console: const y = undefined; y == null; It should return true

    – Seraf

    Mar 18, 2019 at 1:19

  • 3

    @ChrisStryczynski In example from your comment you declared y constant, but you ware comparing abc (not y). When I tested y via console.log(y == null); on Chrome and Firefox I got true as result. If you got error then maybe you tried to use assignment operator = instead of comparison == which would make sense to return error since const can’t be reassigned.

    – Pshemo

    Apr 17, 2019 at 20:50


  • 5

    could be tricky: undefined !== null –> true undefined == null –> true

    – d.popov

    May 14, 2019 at 9:13

  • 4

    live example of relevant comparisons. lgtm.

    Aug 8, 2019 at 17:17

1296

The standard way to catch null and undefined simultaneously is this:

if (variable == null) {
     // do something 
}

–which is 100% equivalent to the more explicit but less concise:

if (variable === undefined || variable === null) {
     // do something 
}

When writing professional JS, it’s taken for granted that type equality and the behavior of == vs === is understood. Therefore we use == and only compare to null.


Edit again

The comments suggesting the use of typeof are simply wrong. Yes, my solution above will cause a ReferenceError if the variable doesn’t exist. This is a good thing. This ReferenceError is desirable: it will help you find your mistakes and fix them before you ship your code, just like compiler errors would in other languages. Use try/catch if you are working with input you don’t have control over.

You should not have any references to undeclared variables in your code.

26

  • 51

    This will cause a ReferenceError and break execution if variable is not defined or referred to at all in the code, using typeof is safer.

    Jun 15, 2014 at 9:49

  • 72

    That’s more of a stylistic point. If the variable hasn’t been declared at all, that’s really just bad writing on the part of the author. You should know whether your variable has been declared or not, that shouldn’t be a question. But yes, if for some reason that’s the case, this should be changed to window.variable instead of just variable, which will not cause a reference error. Typeof should be avoided.

    Jun 15, 2014 at 15:40


  • 6

    Yes because you wrote !== instead of !=.

    Oct 12, 2016 at 15:39

  • 9

    -OP: The statement about those comparisons being “100% equivalent” is SIMPLY WRONG, as you noted in your own EDIT, the second will cause a ReferenceError. As for the assertion: “You should not have any references to undeclared variables in your code.” REALLY? Ever heard of Optional Parameters? jsfiddle.net/3xnbxfsu

    Jan 9, 2017 at 22:04


  • 21

    @TimothyKanski optional parameters may be undefined if they are optionally not provided, but they are most definitely declared variables. They are declared and have a value of undefined, as any declared but uninitialized variable would, ie var someVar; so your argument doesn’t really hold up

    – chiliNUT

    Mar 5, 2017 at 1:14


281

Combining the above answers, it seems the most complete answer would be:

if( typeof variable === 'undefined' || variable === null ){
    // Do stuff
}

This should work for any variable that is either undeclared or declared and explicitly set to null or undefined. The boolean expression should evaluate to false for any declared variable that has an actual non-null value.

8

  • 2

    @Aerovistae I recognize that typeof is an operator, not a function, so it doesn’t need the parentheses, but I appreciate the parentheses nonetheless – simply for reading clarity.

    Sep 4, 2014 at 23:28


  • 1

    what about directly checking if(variable===undefined) instead of using typeof?

    Mar 5, 2015 at 13:45

  • 3

    @ArjunU that will cause a ReferenceError if the variable isn’t declared. If you don’t know whether or not a variable is declared, used the above solution. If you can guarantee that the variable is at least declared, you can use variable == null

    – Rogue

    Mar 31, 2015 at 16:04

  • 3

    This is a better solution because as @Rogue pointed out, the variable might not be declared.

    May 13, 2016 at 11:13

  • Correct me if I am wrong, but isn’t the first conditional a superset of the second one, and therefore the second conditional is superfluous?

    – March Ho

    May 31, 2016 at 3:03