Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeof usage for undefined variables

what is the best usage for the "typeof" JavaScript function?

if (typeof (myvar) == 'undefined') { 
//or
if (typeof (myvar) == undefined) { 
//or
if (typeof myvar == 'undefined') { 
//or
if (typeof myvar == undefined) { 

Thanks

like image 544
Tech4Wilco Avatar asked Oct 07 '11 17:10

Tech4Wilco


1 Answers

typeof is an operator, not a function, and returns a string; so do not use parentheses and do compare it to a string.

When you compare things, avoid type coercion unless you need it (i.e. use === not ==).

if (typeof myvar === 'undefined') { 
like image 106
Quentin Avatar answered Oct 03 '22 18:10

Quentin