11

Possible Duplicate:
What is the !! operator in JavaScript?

What is a not not in javascript I have seen this a few times :)

function foo(){
    return !!(window.history);
}
Community
  • 1
  • 1
Val
  • 17,336
  • 23
  • 95
  • 144
  • 2
    Exact duplicate: http://stackoverflow.com/questions/784929/what-is-the-operator-in-javascript – ig0774 Mar 23 '11 at 11:44
  • 1
    It's a duplicate, but that duplicate question barely mentions why it's used. In your case, it's [the bottom answer](http://stackoverflow.com/questions/784929/what-is-the-operator-in-javascript/4339463#4339463): it's an idiom to check 'is defined'. – Rup Mar 23 '11 at 11:48
  • @rup I agree the answers there pretty wack, I didnt understand half of them answers and their technical B-S, they should be more friendly with their answer and not assume everyone is on their level :) especially when someone asks :) – Val Mar 23 '11 at 12:05
  • The title of this question is much easier to find then the duplicate one – Ido Ran Sep 10 '11 at 11:17

2 Answers2

12

i believe it is used for enforcing boolean types...

for example

if("true" == true){
    alert("1");
}else{
    if(!!"true" == true){
        alert("2");
    }
}

alerts 2 not 1

sharpner
  • 3,857
  • 3
  • 19
  • 28
5

I think it used to [not so] implicitly cast value to boolean type

n0rd
  • 11,850
  • 5
  • 35
  • 56