开发者

typeof usage for undefined variables

开发者 https://www.devze.com 2023-04-11 06:27 出处:网络
what is the best usage for the \"typeof\" JavaScript function? if (ty开发者_如何学Gopeof (myvar) == \'undefined\') {

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

if (ty开发者_如何学Gopeof (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 (i.e. use === not ==).

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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消