0

I am reading "Discover Meteor" at the moment, In chapter 7 is has code:

Posts.allow({
  insert: function(userId, doc) {
    // only allow posting if you are logged in
    return !! userId;                        ///// <<==== what does "!!" means?
  }
});

Thanks

Dan Dascalescu
  • 110,650
  • 40
  • 276
  • 363
CodeFarmer
  • 2,326
  • 18
  • 30

3 Answers3

3

Beautifully summed up by Tom Ritter as

// Maximum Obscurity:
val.enabled = !!userId;

// Partial Obscurity:
val.enabled = (userId != 0) ? true : false;

// And finally, much easier to understand:
val.enabled = (userId != 0);

therefore doing casting to a boolean and then doing double negation

Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
1

! will turn any positive value, true, or existing variable(such as strings and arrays) into a false, and any negative, undefined, null, or false into a true. !! applies it twice.

In this context it would be returning true if the variable userId exists and is not empty, null, or false.

Grallen
  • 1,459
  • 1
  • 13
  • 16
0

It just likes you change variable type to boolean

!! userId;

// same as

userId ? true:false;
Chokchai
  • 1,584
  • 11
  • 11