0

Is there a difference between doing

if(something === undefined) {
    //handle
}

and

if(typeof(something) === "undefined") {
    //handle
}

please? Which is better to use and why? Are there any risks involved?

Dropout
  • 13,653
  • 10
  • 56
  • 109
  • It is generally considered better to use the 1st method from all I've read, which solves the issue of nulls if you do not use them and the double equals check. Has its own downsides though – Dellirium Jul 28 '16 at 09:26
  • Note that strict comparison `===` is not necessary in the second case, since `typeof` will always return string – Yosvel Quintero Jul 28 '16 at 09:30
  • Before ES5 you could change the value of `undefined`: `undefined = something; console.log(something === undefined) // -> true` With ES5 `undefined` [isn't writable anymore](http://www.ecma-international.org/ecma-262/6.0/#sec-undefined) – Andreas Jul 28 '16 at 09:39

0 Answers0