I want to know the logic of the following operators
let test = ! + [];
console.log(test); //true
Why?
I can't test !
in any way
typeof ! //ERROR
! && true //ERROR
!
is an operator like +
.
If you're going to do typeof +
you'll get the same error.
Operators can't be used like that.
The reason why let test = ! + [];
worked is because of the order of operation (operator precedence), and it determined the following order:
[]
;+[] //0
;!0 //true
.So, in expr !+[]
, +[]
was executed first, that is why Quentin pointed to that dupe
Read more about expressions and operators on JS MDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With