1

I had been checking for undefined values myself like:

if(variable !== 'undefined')

then I came across the following code:

if(typeof variable1 !== 'undefined' || typeof variable2 !== 'undefined')

I followed the same convention knowing it's safer when variable goes undeclared. While in my code reviews, it was pointed I could simple write:

if(variable1 || variable2)

Which of these is the most standard way to check undefined?

  • Always go for approach 2 – Saima Haji Jan 03 '20 at 05:57
  • 1
    Are you talking about _undeclared_ variables or the value `undefined`? Because these approaches aren't interchangeable if you're asking about the former – Xetera Jan 03 '20 at 05:59
  • never pass undefined variables to your functions by using functional programming versus procedural programming. – mike510a Jan 03 '20 at 06:01
  • 1
    `if(variable !== 'undefined')` will *never* work, because `'undefined'` the string is not the same as the primitive value `undefined`. `if(variable1)` works if the variable, if defined, will always be truthy – CertainPerformance Jan 03 '20 at 06:03

2 Answers2

2

There are various use case as following

  1. if (variable) is standard way to check truthiness of any variable in javascript. You can examples of what values will be truthy on Truthy | MDN Web Docs. Also, Falsy | MDN Docs

  2. The cases where you explicitly would check for undefined is when a variable has been declared but not assigned value or explicitly assigned undefined. In that case use if (variable !== undefined).

  3. If you are receiving response from an API which might consist of stringified value of undefined, which you are sure of, then only do the check if (variable !== 'undefined')

kushdilip
  • 7,216
  • 3
  • 21
  • 30
0

I personally use

myVar === undefined

Warning: Please note that === is used over == and that myVar has been previously declared (not defined).

I do not like typeof myVar === "undefined". I think it is long winded and unnecessary. (I can get the same done in less code.)

Now some people will keel over in pain when they read this, screaming: "Wait! WAAITTT!!! undefined can be redefined!"

Cool. I know this. Then again, most variables in Javascript can be redefined. Should you never use any built-in identifier that can be redefined?

If you follow this rule, good for you: you aren't a hypocrite.

The thing is, in order to do lots of real work in JS, developers need to rely on redefinable identifiers to be what they are. I don't hear people telling me that I shouldn't use setTimeout because someone can

window.setTimeout = function () {
    alert("Got you now!");
};

Bottom line, the "it can be redefined" argument to not use a raw === undefined is bogus.

(If you are still scared of undefined being redefined, why are you blindly integrating untested library code into your code base? Or even simpler: a linting tool.)

Also, like the typeof approach, this technique can "detect" undeclared variables:

if (window.someVar === undefined) {
    doSomething();
}

But both these techniques leak in their abstraction. I urge you not to use this or even

if (typeof myVar !== "undefined") {
    doSomething();
}

Consider:

var iAmUndefined; To catch whether or not that variable is declared or not, you may need to resort to the in operator. (In many cases, you can simply read the code O_o).

if ("myVar" in window) {
    doSomething();
}

But wait! There's more! What if some prototype chain magic is happening…? Now even the superior in operator does not suffice. (Okay, I'm done here about this part except to say that for 99% of the time, === undefined (and ****cough**** typeof) works just fine. If you really care, you can read about this subject on its own.)