2

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

Kevin
  • 209
  • 2
  • 11

2 Answers2

2

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).

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • 1
    hey thanks. how to read about precedence and associativity of operators in here ? – Kevin Aug 10 '20 at 07:33
  • 1
    I don't get the question – Jonas Wilms Aug 10 '20 at 10:28
  • i mean how can i understand about precedence and associativity of ECMAScript operators reading grammars of operators – Kevin Aug 10 '20 at 10:56
  • so, in order to understand the precedence of one operator on other one, we should read all ecmaScript documentation on both, and do some evaluations, apply some deduction, and so on for all the params we are interested in? In order to see what operator is the first in the list, we need to read all the documentation on all the operators ? – serge Feb 21 '22 at 10:46
  • @serge if by "understand" you mean "derive operator precedence from the specification" then yes. However you can also take a spec compliant JavaScript parser and look at the resulting syntax tree, MDN also has a great overview of operator precedence of common operators – Jonas Wilms Feb 21 '22 at 14:13
  • see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence – Jonas Wilms Feb 21 '22 at 14:13
-2

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

Rehan Sattar
  • 3,497
  • 4
  • 13
  • 21