双重感叹是什么!!运营商意味着[重复]

时间:2022-08-28 22:28:50

Possible Duplicate:
What is the !! operator in JavaScript?
What does !! (double exclamation point) mean?

可能重复:什么是!! JavaScript中的运算符?什么!! (双感叹号)是什么意思?

I am going through some custom JavaScript code at my workplace and I am not able to understand the following construct.

我在我的工作场所经历了一些自定义JavaScript代码,我无法理解以下构造。

var myThemeKey = (!!$('row') && $('row').hasClassName('green-theme')) ? 'green' : 'white';

I understand everything on the above line except !! operator. I assume that it is a NOT operator and NOT of NOT is the original value but why would someone do a NOT of NOT?

我理解上面所有的一切,除了!!运营商。我假设它是一个NOT运算符而NOT不是原始值,但为什么有人会做NOT not not?

Can someone please help me understand what is happening on the above line of code?

有人可以帮我理解上面代码行中发生的事情吗?

1 个解决方案

#1


68  

The !! ensures the resulting type is a boolean (true or false).

!!确保结果类型是布尔值(true或false)。

javascript:alert("foo") --> foo

javascript:alert(“foo”) - > foo

javascript:alert(!"foo") --> false

javascript:alert(!“foo”) - > false

javascript:alert(!!"foo") --> true

javascript:alert(!!“foo”) - > true

javascript:alert(!!null) --> false

javascript:alert(!! null) - > false

They do this to make sure $('row') isn't null.

他们这样做是为了确保$('row')不为null。

It's shorter to type than $('row') != null ? true : false.

键入比$('row')更短!= null?真假。

#1


68  

The !! ensures the resulting type is a boolean (true or false).

!!确保结果类型是布尔值(true或false)。

javascript:alert("foo") --> foo

javascript:alert(“foo”) - > foo

javascript:alert(!"foo") --> false

javascript:alert(!“foo”) - > false

javascript:alert(!!"foo") --> true

javascript:alert(!!“foo”) - > true

javascript:alert(!!null) --> false

javascript:alert(!! null) - > false

They do this to make sure $('row') isn't null.

他们这样做是为了确保$('row')不为null。

It's shorter to type than $('row') != null ? true : false.

键入比$('row')更短!= null?真假。