What is the purpose of using !! in JavaScript?
This question already has an answer here:
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:
这不是具体到角,它提供像变换非布尔值undefined
为布尔值。
上一篇: 为什么在使用&时在IF语句中有两个!!