Type of use for variables undefined

What is the best use for typeof javascript function?

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

thanks

+7
source share
2 answers

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

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

 if (typeof myvar === 'undefined') { 
+14
source

Use strict comparison ( === ) and quote "undefined" :

 if (typeof myvar === "undefined") {} 
+4
source

All Articles