Multiplication Before Addition
What you'll learn: Why 2 + 3 * 4 equals 14 (not 20), and how JavaScript respects mathematical order of operations.
The Core Idea
JavaScript follows the same arithmetic rules you learned in math class. When an expression contains both multiplication and addition, multiplication happens first — even if addition appears earlier in the code.
This is because of operator precedence: multiplication has a higher precedence level than addition, so JavaScript evaluates it before moving on to the addition.
How It Works in Practice
let result = 2 + 3 * 4;
console.log(result); // 14
JavaScript doesn't read this left-to-right as "2 plus 3 equals 5, times 4 equals 20." Instead, it follows precedence rules:
- First:
3 * 4= 12 - Then:
2 + 12= 14
If you wanted the addition to happen first, you'd use parentheses (which have even higher precedence):
let result = (2 + 3) * 4;
console.log(result); // 20
The Analogy
Think of precedence like a to-do list with priority levels. Multiplication is marked "urgent" while addition is "normal priority." JavaScript handles all urgent tasks first, then works through the rest.
let x = 10 + 2 * 5; // 10 + 10 = 20
let y = 100 - 4 * 3; // 100 - 12 = 88
let z = 5 + 6 - 2 * 3; // 5 + 6 - 6 = 5
Key Takeaway: Multiplication (and division) always execute before addition (and subtraction) in JavaScript, following mathematical precedence rules. Use parentheses when you need different behavior.