What does the double exclamation !! operator mean?

Javascript

Javascript Problem Overview


> Possible Duplicate:
> What is the !! operator in JavaScript?
> What does !! (double exclamation 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?

Javascript Solutions


Solution 1 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstirfriesView Question on Stackoverflow
Solution 1 - Javascripti_am_jorfView Answer on Stackoverflow