Value!! Javascript

I found authentication code with angular, and I cannot figure out this trick:

authService.isAuthenticated = function () { return !!Session.userId; }; 

What does it mean !! does it mean "different from userId"?

whenever true = !!true = !!!!true =>etc , he does not understand this.

Can anybody help me?

( https://medium.com/opinionated-angularjs/techniques-for-authentication-in-angularjs-applications-7bbf0346acec for the source, part of 'AuthService')

+7
javascript angularjs
source share
4 answers

!! Converts any value to a boolean value

  > !!null false > !!true true > !!{} true > !!false false 

If falsey , then the result will be false . If it is truthy , the result will be true .

Moreover, the third ! inverts the converted value, so the above examples become:

  > !!!null true > !!!true false > !!!{} false > !!!false true 
+11
source share

It forces what returns as a boolean , not an integer or empty value. For example, 0 evaluates to false with == , but will not with === . Therefore, to return any integer 0 , will be converted to a boolean, we use !! . This also works if null or undefined returned.

So what really happens is:

 var test = null; var result = !test; // returns true result = !return; // returns false 
+2
source share

!! used to convert the value to the right of it to its equivalent logical value.

 !!false === false !!true === true 
+1
source share

Confirms that oObject is set to boolean. If it was false (e.g. 0, null , undefined , etc.), it will be false , otherwise true .

 !oObject //Inverted boolean !!oObject //Non inverted boolean so true boolean representation 

So!! this is not an operator, it's easy! operator twice.

Referrals: fooobar.com/questions/478 / ...

0
source share

All Articles