CodeFarmer
CodeFarmer

Reputation: 2708

What does this syntax means -----> " !! " in javascript?

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

Upvotes: -2

Views: 152

Answers (3)

Chokchai
Chokchai

Reputation: 1722

It just likes you change variable type to boolean

!! userId;

// same as

userId ? true:false;

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44834

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

Upvotes: 3

Grallen
Grallen

Reputation: 1670

! 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.

Upvotes: 1

Related Questions