+ 2
what does " !! "mean in this code ? and what does the first if statement mean?
var num = 20; if ( !! num || ! num && num - num){ console.log("A"); } else if (num == 20) { console.log("C") } else { console.log("B"); }
3 Answers
+ 4
! = NOT
! true = false
! false = true
!! = NOT NOT
!! true = true
!! false = false
!!! true = false
!!! false = true
https://medium.com/@edplatomail/js-double-bang-or-the-not-operator-part-40e55d089bf0
!! num returns true.
! num returns false
|| OR (true || true = true, false || true = true, true || false = true)
&& AND (only true for true && true)
if(true OR false && 0)
false AND 0 returns false
true || false returns true
-> the whole condition in if statement is true
output: A
But don't ask me about the sense of such a condition ;)
Edit: Maybe it is just an exercise about boolean
+ 6
!!X === !(!X) === Boolean(X)
!X === !(Boolean(X))
For me the first if statement has not meaning.
+ 2
A trick to convert a number to its boolean form.