4

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
Adelin
  • 7,809
  • 5
  • 37
  • 65
Zhang YH
  • 41
  • 3

1 Answers1

9

! 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:

  1. evaluate [];
  2. convert it to a number with +[] //0;
  3. negate that conversion with !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

Adelin
  • 7,809
  • 5
  • 37
  • 65