简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM