Justin
Justin

Reputation: 45340

In Javascript, is typeof var !== undefined equivalent to if(var)

In Javascript, is:

if(typeof p_options.data_type !== "undefined") { }

Absolutely equivalent to just doing:

if(p_options.data_type) { }

Are there any edge cases or gotchas?

Upvotes: 1

Views: 52

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149000

No. It definitely is not the same.

Imagine if p_options.data_type was false, 0, "", or any other "falsey" value. This is very different from undefined.

p_options.data_type = false;

console.log(typeof p_options.data_type !== "undefined"); // true
console.log(p_options.data_type);                        // false

Upvotes: 4

Related Questions