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
  • 29,463
  • 31
  • 100
  • 148
Benny7500
  • 569
  • 6
  • 16
  • 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
  • 57,230
  • 10
  • 89
  • 128
2

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

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
Satpal
  • 132,252
  • 13
  • 159
  • 168
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,487
  • 2
  • 24
  • 46