Why This Matters
Write 2 + 3 * 4 in any programming language and you get 14, not 20. Why?
Because of the order of operations: a set
of rules that determine which parts of an expression
get evaluated first. Without these rules, the same expression could produce
different results depending on who reads it. Mathematics and programming both
need unambiguous evaluation.
The mnemonic PEMDAS captures the priority: Parentheses first, then Exponents, then Multiplication and Division (left to right), then Addition and Subtraction (left to right). This is not an arbitrary convention -- it reflects the mathematical structure of operations. Multiplication binds more tightly than addition because it represents repeated addition, a more fundamental grouping.
Every programming language follows these rules. Every spreadsheet formula obeys them. Every calculator applies them. When your code produces an unexpected number, the first thing to check is whether you have the order of operations right. Parentheses are your tool for overriding the default order when you need a different evaluation sequence.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
PEMDAS determines evaluation order: Parentheses, Exponents, Multiply/Divide, Add/Subtract. Parentheses let you override the default.
Code Example
// Order of operations in action
console.log(2 + 3 * 4); // 14, not 20
console.log((2 + 3) * 4); // 20 (parentheses override)
// PEMDAS step by step
// Expression: 2 + 3 * 4 ** 2
console.log(4 ** 2); // 16 (exponents first)
console.log(3 * 16); // 48 (then multiplication)
console.log(2 + 48); // 50 (then addition)
console.log(2 + 3 * 4 ** 2); // 50 (all at once)
// Left-to-right for same precedence
console.log(20 - 5 + 3); // 18 (left to right: 15 + 3)
console.log(20 / 5 * 2); // 8 (left to right: 4 * 2)
// Nested parentheses: innermost first
console.log(((2 + 3) * (4 - 1)) + 1); // 16
// Step 1: (2+3) = 5, (4-1) = 3
// Step 2: 5 * 3 = 15
// Step 3: 15 + 1 = 16
// Common mistake: forgetting precedence
const price = 100;
const tax = 0.08;
console.log(price + price * tax); // 108 (correct)
console.log((price + price) * tax); // 16 (wrong!)
// Simple expression evaluator
function evaluateSimple(a, op1, b, op2, c) {
if (op2 === "*" || op2 === "/") {
const right = op2 === "*" ? b * c : b / c;
return op1 === "+" ? a + right : a - right;
}
const left = op1 === "+" ? a + b : a - b;
return op2 === "+" ? left + c : left - c;
}
console.log(evaluateSimple(2, "+", 3, "*", 4)); // 14Interactive Experiment
Try these exercises:
- Evaluate
10 - 2 * 3 + 1by hand, then check with code. Did you get 5? - Add parentheses to
2 + 3 * 4to make it equal 20 instead of 14. Where do the parentheses go? - Evaluate
2 ** 3 ** 2in Python. Is it 64 or 512? Exponentiation is right-to-left associative. Why? - Write the expression for "the average of 10, 20, and 30" using only arithmetic operators and parentheses. What happens without the parentheses?
- Create an expression with all four operations (+, -, *, /) that evaluates to exactly 1.
Quick Quiz
Coding Challenge
Write a function called `evaluateExpression` that takes three numbers (a, b, c) and two operators (op1, op2), and evaluates the expression 'a op1 b op2 c' following correct PEMDAS order. Operators will be '+', '-', '*', or '/'. Multiplication and division have higher precedence than addition and subtraction. Same-precedence operators are evaluated left to right. Return the numeric result.
Real-World Usage
Order of operations governs all computation:
- Spreadsheet formulas: Excel and Google Sheets follow PEMDAS exactly. A formula like
=A1+B1*C1multiplies before adding. Getting the wrong answer usually means missing parentheses. - Programming languages: Every language (JavaScript, Python, C, Java) follows the same precedence rules. Operator precedence tables are part of every language specification.
- SQL queries: Database expressions like
WHERE price + tax * rate exceeds 100apply the same precedence. Understanding this prevents subtle query bugs. - Scientific computing: Physics formulas like
F = m * aandE = m * c ** 2rely on correct evaluation order. One misplaced parenthesis changes the physics. - Financial formulas: Compound interest:
P * (1 + r/n) ** (n*t). Every parenthesis matters. Without them, the formula gives a completely different (wrong) result.