简体   繁体   English

JavaScript未定义变量检测:typeof ===“undefined”vs双重感叹号

[英]JavaScript undefined variable detection: typeof===“undefined” vs double exclamation

Common practice when checking whether a variable is undefined in JavaScript is as follows: 检查JavaScript中的变量是否未定义时的常见做法如下:

var isUndefined=(typeof variableName==="undefined");


I've recently come across the use of two exclamation symbols to determine the same logic: 我最近遇到使用两个感叹号来确定相同的逻辑:

var isUndefined=!!variableName;



Is it safe to use these methods interchangeably? 交替使用这些方法是否安全?
Are both equally compatible across browsers? 两者在浏览器中是否兼容?
Is there any reason why one shouldn't use the "!!" 是否有人不应该使用“!!” method? 方法? (It seems more concise and easy to read) (看起来更简洁易读)

Is it safe to use these methods interchangeably? 交替使用这些方法是否安全?

No. Details below, but they're not interchangeable even if you remove one of the ! 不。详情如下,但即使你删除其中一个,它们也不可互换! from your second example (with two of them, it will be true for undefined , which isn't what you seem to want from the name of the variable you're assigning to). 从你的第二个例子(其中有两个,对于undefined来说,这将是true ,这不是你想要从你指定的变量名称中得到的)。 But that's not the only issue. 但这不是唯一的问题。

Are both equally compatible across browsers? 两者在浏览器中是否兼容?

Yes. 是。 Each of them works reliably across browsers. 它们中的每一个都可以跨浏 But see above: They reliably don't do the same thing. 但是见上文:他们可靠地不做同样的事情。

Is there any reason why one shouldn't use the "!!" 是否有人不应该使用“!!” method? 方法?

Yes, ! 是的, ! (again, not !! ) gives the same result for 0 , NaN , null , "" , and false as it does for undefined . (再次,不!!给出与0NaNnull""false相同的结果,就像它对undefined

Details: 细节:

When you do 当你这样做

var isUndefined = typeof variableName==="undefined";

(the () are unnecessary) , you're doing a very specific test for undefined . ()是不必要的) ,你正在为undefined做一个非常具体的测试。 It will only be true for undefined . 只有undefined才会true

In contrast, when you do 相比之下,当你这样做

var isUndefined = !variableName;

you're not testing for undefined , you're testing for any falsey value . 你没有测试undefined ,你正在测试任何虚假值 The falsey values are the ones I listed earlier ( 0 , NaN , null , "" , false , and undefined ). 假名值是我之前列出的值( 0NaNnull""falseundefined )。 The result will be true for any of them. 结果将true于任何一个。

Now, if you're expecting (for instance) to get a non- null object reference or undefined , the ! 现在,如果你期望(例如)获得非null对象引用或undefined ,那么! test is just fine. 测试很好。 But if you really need to know whether something is undefined , specifically, you want the typeof test. 但是,如果你真的需要知道的东西是否是undefined ,特别是,你想要的typeof测试。

Also, as Felix Kling pointed out in a comment on the question, the ! 另外,正如Felix Kling在对这个问题的评论中指出的那样! test will throw a ReferenceError if the variable isn't defined at all (as opposed to being defined but having the value undefined ), because it tries to read the value of the variable. 如果根本没有定义变量(与定义但undefined值相反),test将抛出一个ReferenceError ,因为它试图读取变量的 The typeof test is safe if you're not sure whether the variable exists at all, because it doesn't try to read its value. typeof ,如果你不知道的变量是否存在可言,因为它不尝试读取其值试验是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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