1

In Meteor tutorial project Microscope there is row with "!!" operator

Posts.allow({
  insert: function(userId, doc) {
  return !! userId; 
 }
});

What does it mean?

Mild Fuzz
  • 27,845
  • 29
  • 97
  • 148
Benny7500
  • 549
  • 6
  • 15
  • 2
    The principle behind this is called type casting and it's already covered by many questions. – Boaz Feb 14 '14 at 15:39

4 Answers4

3

It returns userId as a boolean

Sam
  • 166
  • 5
3

It's a boolean cast, from a fasly or truethy value to false or true respectively.

You might see:

var string = ""; // empty string is falsy
var bool = !!string; // false

falsy by the way means that it follows: myvar == false as opposed to false which follows: myvar === false (triple comparison).

Similarly truethy is myvar == true.

Halcyon
  • 56,029
  • 10
  • 87
  • 125
2

!!userId is the same as Boolean(userId).

Mathletics
  • 33,673
  • 6
  • 49
  • 57
Satpal
  • 129,808
  • 12
  • 152
  • 166
1

The ! operator inverts a boolean, so by inverting a value two times, you get the same boolean. So true === !!true.

This often is used to convert a value into a boolean, as ! is guaranteed to return a boolean.

Lars Ebert
  • 3,407
  • 2
  • 21
  • 44