-1

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

2

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

It just likes you change variable type to boolean

!! userId;

// same as

userId ? true:false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.