开发者

What does the double exclamation !! operator mean? [duplicate]

开发者 https://www.devze.com 2023-04-05 17:29 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: What is the !! operator in JavaScript?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What is the !! operator in JavaScript?

What does !! (double exclama开发者_C百科tion point) mean?

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

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?

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


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

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

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

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

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

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消