can someone explain me where in the ECMAScript specification operator precedence and operator associativity is mentioned

Viewed 151

I was reading mdn docs about operator precedence and operator associativity "operator precedence and operator associativity(MDN)" and wanted to know more about it reading the ECMAScript specification.

But i didn't find anything about operator precedence and operator associativity in there.

Can someone guide me with a link to the ECMAScript specification where they describe precedence and associativity of each operator.

Any help is really appreciated. And i need to know if the ECMAScript specification doesn't mention anything about

precedence and associativity of each operator how language implementers know which operator to resolve first before the other i mean how they know which operator should get evaluated before the other operator

2 Answers

As an example, the operator predescendence of multiplication over addition is in section 12.8 of the specification

12.8 Additive Operators Syntax

  AdditiveExpression:
      MultiplicativeExpression
      AdditiveExpression + MultiplicativeExpression
      AdditiveExpression - MultiplicativeExpression

edited for readability

Because of these productions 1 + 2 * 3 gets produced through an AdditiveExpression, with two MultiplicativeExpressions inside:

    AdditiveExpression
    (AdditiveExpression + MultiplicativeExpression)
    ((MultiplicativeExpression) + (MultiplicativeExpression MultiplicativeOperator MultiplicativeExpression))
    //...
    ((1) + (2 * 3))

If you evaluate this, the MultiplicativeExpressions get evaluated first (see section 12.8.3.1).

Usually we learn all these things in a course named "Compiler Design". In this course we explore how these rules are created. What are the levels and association of operators.

These rules are not specific to JavaScript only. Some languages have same rules, some has different. If you want to learn how these rules are created, I would recommend you to learn some basics of Compiler Design.

For understanding the concepts, I always refer https://javascript.info/operators

Related