1

I am going thru firebase tutorial / codelabs

I came across a construct like following;


// Returns true if a user is signed-in.
function isUserSignedIn() {
  return !!firebase.auth().currentUser;
}

in that code what does !! mean ? does 2 ! cancels each other. If it does cancel why'd have author done !! ?

forvaidya
  • 2,255
  • 2
  • 17
  • 24
  • 1
    Consider that `currentUser` is not a boolean. – Bergi Feb 17 '19 at 15:11
  • can you please elaborate ? does it mean that !user may or may not be Boolean and 2nd ! guarantees that it is Boolean ? – forvaidya Feb 17 '19 at 15:12
  • 1
    They don't always cancel out each other - they also make a type conversion. Sure, `!user` and `!!user` are always boolean, but `user` need not be. – Bergi Feb 17 '19 at 15:13
  • 1
    `!!` is called double negate operator, which in this case makes a boolean type coercion, which means that it turns any value into a boolean. In order to do that, makes a truthy/falsy conversion first. Check this for full explanation: https://www.oreilly.com/library/view/you-dont-know/9781491905159/ch04.html#explicitly----boolean – Chris Tapay Feb 17 '19 at 15:13
  • Thanks All ; this helps – forvaidya Feb 17 '19 at 15:14
  • https://medium.com/@edplatomail/js-double-bang-or-the-not-operator-part-40e55d089bf0 ??? – forvaidya Feb 17 '19 at 15:15
  • Does this means that !! is a coding style and not part of syntax and in theory I can do any numbers of ! -- like !!!!!!thisthing.tatthing – forvaidya Feb 18 '19 at 06:34

0 Answers0