#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Operator precedence and associativity

Operator precedence decides which operator in an expression runs first, and associativity decides the order when operators share the same precedence. Together they explain why 2 + 3 * 4 is 14 rather than 20, and why some expressions give results you did not expect. When in doubt, parentheses override both rules.

Precedence: who goes first

Operators with higher precedence bind more tightly, so they are evaluated before lower ones. Multiplication beats addition, comparison beats logical AND, and AND beats OR. Here is a simplified order from highest to lowest:

LevelOperatorsAssociativity
Grouping( )n/a
Member access, calls. [] () ?.left to right
Postfix / prefixx++, ! typeof -x ++xn/a / right to left
Exponent**right to left
Multiplicative* / %left to right
Additive+ -left to right
Relational< > <= >= instanceof inleft to right
Equality== != === !==left to right
Logical AND&&left to right
Logical OR, nullish|| ??left to right
Conditional? :right to left
Assignment= += -= and friendsright to left

Associativity: same level, which side first

Most operators are left-associative: 10 - 4 - 3 is read as (10 - 4) - 3, which equals 3. A few are right-associative. Exponentiation is one: 2 ** 3 ** 2 means 2 ** (3 ** 2), which is 512, not 64. Assignment is another, which is why a = b = 5 sets b first and then copies the result into a.

console.log(2 + 3 * 4);          // 14   (* before +)
console.log((2 + 3) * 4);        // 20   (parentheses first)
console.log(2 + 3 * 4 ** 2);     // 50   (** then * then +)
console.log(2 ** 3 ** 2);        // 512  (right to left)
console.log(10 - 4 - 3);         // 3    (left to right)

const age = 20, member = false;
console.log(age > 18 && member || age > 60);  // false
// read as: ((age > 18) && member) || (age > 60)

console.log(1 + 2 + "3");        // "33"  (3 + "3")
console.log("1" + 2 + 3);        // "123" (string from the start)

Precedence is not evaluation order

A subtle point: precedence decides how operators are grouped, not which operand is computed first. JavaScript always evaluates operands from left to right. In a() + b() * c(), the three functions are called in the order a, b, c, and only then is the multiplication done before the addition. Short-circuit operators can skip evaluating the right side entirely.

Expressions that catch people out

Mixing strings and numbers is the most common surprise. Because + is left-associative, 1 + 2 + "3" adds the numbers first and gives "33", while "1" + 2 + 3 becomes string concatenation from the very first step and gives "123".

JavaScript also refuses some ambiguous combinations outright. -2 ** 2 is a syntax error, because it is unclear whether you mean (-2) ** 2 or -(2 ** 2); you must add brackets. Likewise, mixing ?? with || or && without parentheses throws a syntax error.

Another classic is !a instanceof B, which is read as (!a) instanceof B and is almost always false. Write !(a instanceof B).

A practical rule

You do not need to memorise the whole table. Know that maths follows normal school rules, that AND binds tighter than OR, and that assignment comes last. For everything else, add parentheses. They cost nothing and make your intent obvious to the next reader.

Takeaway: precedence groups operators, associativity breaks ties at the same level, operands still run left to right, and parentheses are the cleanest way to remove any doubt.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course