Javascript 双感叹号是什么!!运营商是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7452720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:21:35  来源:igfitidea点击:

What does the double exclamation !! operator mean?

javascript

提问by stirfries

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 NOToperator and NOTof NOTis the original value but why would someone do a NOTof NOT?

除了!!操作符,我理解上面一行的所有内容。我假设它是一个NOT运算符,而NOTofNOT是原始值,但为什么有人会做 a NOTof NOT

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

有人可以帮我理解上面的代码行发生了什么吗?

回答by i_am_jorf

The !!ensures the resulting type is a boolean (true or 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')不为空。

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

输入比$('row') != null ? true : false.