You are currently viewing What is Associativity of Operators in JS

What is Associativity of Operators in JS

JS Associativity of Operators

In this article, we will see the associativity of operators in js.

Associativity of operators

If the same operator appears one or more times in an expression, in which direction the specific operator needs to be evaluated is determined by the associativity of operators.

While evaluating any given expression if operators appear multiple times whether that operator should be evaluated from left to right or from right to left is determined by the associativity operator.

Ex:

var result = 3 * 5 + 4 / 2 + 3 + 20 -10

Note: Except for unary and assignment operators; All javascript operators have left-to-right associativity. That means unary and assignment operators should be evaluated from right to left and other operators should be evaluated from left to right. For example, the plus (+) operator has left to right associated, so we have to evaluate the plus operators from left to right.

Example:

var result = 2 + 2 + 4 + 2;
var a = b = c = 10;

Here we have an assignment operator which is appearing three times and it has right to left associative, so we have to evaluate it from right to left. 10 is assigned to c, c is assigned to b, b is assigned to a.

Leave a Reply