Mastering Operator Precedence in JavaScript – TCS Interview Code Explained


Our Javascript Code:

console.log(10+2*3);

Step-by-Step Breakdown

Step 1: Understand the Operators

You're using two arithmetic operators:

Operator        Description        Precedence        Associativity
    *        Multiplication        Higher than +        Left-to-right
    +        Addition        Lower than *        Left-to-right

Step 2: Apply Operator Precedence

JavaScript looks at the operator precedence and starts with the higher one.

Expression:

10 + 2 * 3

First, multiplication (*) has higher precedence than addition (+), so:

2 * 3 = 6

Now the expression becomes:

10 + 6 = 16

✔ Final Answer:

A) 16

This question appeared in the TCS (Tata Consultancy Services) interview, testing the candidate’s knowledge of operator precedence  in JavaScript.


Remember key Concept: Operator Precedence

JavaScript follows a strict order of operations just like in math (remember PEMDAS?):

Parentheses
Exponents
Multiplication
Division
Addition
Subtraction

So in this case:

  • Multiplication happens first (2 * 3 = 6)
  • Then addition happens (10 + 6 = 16)

Post a Comment

Previous Post Next Post