I want to know the purpose of using !!
in JS.
For ex:
this.enabled = function (value) {
if (arguments.length) {
enabled = !!value;
}
}
I want to know the purpose of using !!
in JS.
For ex:
this.enabled = function (value) {
if (arguments.length) {
enabled = !!value;
}
}
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
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:
It's not specific to Angular, it serves to transform a non-boolean value like undefined
to boolean.