Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

Precedence & associativity of operators

Notes:

Precedence & Associativity of JavaScript Operators :

Precedence:
While evaluating a given expression; which operator should be evaluated first before the other?
I.e. which operator needs to be given higher precedence than the other?
Note: Lower the precedence number higher the precedence

Associativity:
If same operator appears one or more times in an expression, in which direction the specific operator(s) need to be evaluated

a = 3 * 5 + 4 / 2 + 3 + 20 -10

Precedence and associativity of JavaScript operators table:

P. Level Precedence
1 P: Parenthesis
2 U: Unary (Right to Left associativity)
3 M: Multiplicative
4 A: Additive
5 S: Shift
6 R: Relational
7 E: Equality
8 B: Bitwise
9 L: Logical
10 C: Conditional
11 A: Assignment (Right to Left associativity)
12 C: Comma

Example code:
var a = 2 + 2 + 2;
document.write(a); // 6
var b = 2 + 3 * 5;
document.write(b); // 17 not 25
var c = 3 * 5 + 4 / 2 + 3;
document.write(c); // 20
var d = 3 * (5 +4) / 2 +3;
document.write(d); // 16.5
var e = 3 * 5 + 4 / 2 + 3 + 20 -10;
document.write(e); // 30

Interview Questions: