[英]typeof usage for undefined variables
what is the best usage for the " typeof " JavaScript function? “ typeof ”JavaScript函数的最佳用法是什么?
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 ; typeof
是一个运算符 ,而不是一个函数,并返回一个字符串 ; 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 ==
). 比较时,除非你需要,否则请避免使用强制类型(即使用
===
not ==
)。
if (typeof myvar === 'undefined') {
使用严格比较( ===
),并引用"undefined"
:
if (typeof myvar === "undefined") {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.