简体   繁体   中英

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

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 (ie use === not == ).

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

使用严格比较( === ),并引用"undefined"

if (typeof myvar === "undefined") {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM