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
  • 143,271
  • 52
  • 317
  • 404
CodeFarmer
  • 2,644
  • 1
  • 23
  • 32

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
  • 44,617
  • 6
  • 35
  • 64
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,620
  • 1
  • 17
  • 16
0

It just likes you change variable type to boolean

!! userId;

// same as

userId ? true:false;
Chokchai
  • 1,684
  • 12
  • 12