1

I want to know the purpose of using !! in JS.

For ex:

this.enabled = function (value) {
   if (arguments.length) {
    enabled = !!value;
    }
}
kero
  • 10,647
  • 5
  • 41
  • 51
prince
  • 601
  • 6
  • 15
  • 30

3 Answers3

5

It's not related to angular

It's just a way to transform value to bool. ( according to truthy/falsy values)

There is a lot of articles about it.

examples :

!!"a" //true

!!"0" //true

!!0 //false

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
3

To be clear, this question has nothing to do with AngularJS--it is a JS syntax question.

The purpose of !! in JavaScript (and other langs) is to force a value to boolean.

Using a single ! forces it to boolean, but opposite of whether the value was "truthy" or "falsy". The second ! flips it back to be a boolean which matches the original "truthy" or "falsy" evaluation.

var a = 'a string';
var f = !a; // f is now boolean false because a was "truthy:
var t = !!a; // f is now boolean true because a was "truthy:
JAAulde
  • 19,250
  • 5
  • 52
  • 63
0

It's not specific to Angular, it serves to transform a non-boolean value like undefined to boolean.

Mik378
  • 21,881
  • 15
  • 82
  • 180