23
fyzrn
8y

true && 1
> 1
true && !!1
> true

Welcome to Javascript

Comments
  • 7
    It's not that surprising since (!1) is 'boolean'
  • 2
    It looks perfectly sane to me. 1 is loosely equal to true. Thus, !1 evaluates to false. !!1 then evaluates to false before negating the expression with the remaining ! to ultimately evaluate to true. Also, logical AND and OR operators loosely evaluates the expressions from left to right which in the first example becomes "true && true"
  • 0
    @wessberg yes, but the thing that made me scratch my head is that true && 1 returns 1 instead of true :/
  • 3
    false && X == false
    true && X == X
    false || X == X
    true || X == true

    easy as that.
    First and last one work even if X throws an error
  • 1
    Is an expresion, not an statement
  • 0
    Why do you even need to do something like true && 1 in your code :-)
  • 1
    @tomabolt I don't, it's for illustration purpose only
Add Comment