[英]JavaScript: difference between typeof(var)==='undefined' vs =='undefined'?
Perhaps a somewhat theoretical question, but to check if a variable exists or not, this is most commonly advised:也许是一个有点理论的问题,但要检查变量是否存在,最常建议这样做:
typeof(var)==='undefined'
or typeof(var)!=='undefined'
typeof(var)==='undefined'
或typeof(var)!=='undefined'
How does this differ from typeof(var)=='undefined'
(or typeof(var)!='undefined'
) ?这与
typeof(var)=='undefined'
(或typeof(var)!='undefined'
)有何不同?
I mean ===
vs ==
.我的意思是
===
与==
。 Or !==
vs !=
.或者
!==
vs !=
。 I know this normally means comparison of type as well as value, but in this case, typeof(something)
always evaluates to a string, right?我知道这通常意味着比较类型和值,但在这种情况下,
typeof(something)
总是计算为字符串,对吗?
Is there any scenario possible where typeof(var)==='undefined'
and typeof(var)=='undefined'
are not the same?是否存在
typeof(var)==='undefined'
和typeof(var)=='undefined'
不相同的情况?
There is really no difference thus typeof returns a string.真的没有区别,因此 typeof 返回一个字符串。
Use ===
and !==
when you want to avoid automatic conversions.当您想避免自动转换时,请使用
===
和!==
。
Examples:例子:
alert(1!='1')//false
alert(1!=='1')//true
alert('1'!='1')//false
alert('1'!=='1')//false
alert(true==1)//true
alert(true===1)//false
'===' 和 '!==' 运算符要快一些,因此在大多数情况下,应该使用它们代替 '==' 或 '!='。
As for the !=
part, the !
至于
!=
部分, !
is to be taken as a NOT.将被视为 NOT。
typeof var !=='undefined'
=> if typeof var is not undefined
typeof var !=='undefined'
=> if typeof var is not undefined
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.