pourmesomecode
pourmesomecode

Reputation: 4318

How does this check work with operator precedence in JS

So I'm reading through this precedence table https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

It says 20 - 1 with 20 being highest precedence.

16 Logical NOT right-to-left ! … So the ! operator has a precedence of 16.

10 Strict Equality … === … So the === operator has a precendence of 10.

This lines

!'hello' === 'goodbye'

How does this get executed/read? By reading it I thought. In steps it goes;

'hello' === 'goodbye' Check then, invert bool value. So If it returns true set it to false.

If i'm reading through the precedence operators table. It looks to me like it does the ! operator first and then ===.

How does it invert a non-bool value beforehand and then do the truthy check? Like how does it work could someone explain?

Thank you!

Upvotes: 1

Views: 152

Answers (1)

Quentin
Quentin

Reputation: 943089

It looks to me like it does the ! operator first and then ===.

Yes. 16 is a higher number than 10, so ! has a higher precedence than ===, so it is resolved first.

How does it invert a non-bool value beforehand and then do the truthy check?

See the spec for ! which points to ToBoolean which says:

String: Return false if argument is the empty String (its length is zero); otherwise return true.

Upvotes: 3

Related Questions