Spot the Math Operation Bug
Question
This code should calculate the average of 10 and 20, but it gives 110. What is wrong?
Code
let avg = 10 + 20 / 2;
console.log(avg);
Hint
Remember operator precedence: / binds tighter than +.
Explanation
Due to operator precedence, division is evaluated before addition. So 10 + 20 / 2 is computed as 10 + (20 / 2) = 10 + 10 = 20, not (10 + 20) / 2 = 15. Wait — actually 10 + 10 = 20, not 110. Let me recalculate: 20 / 2 = 10, then 10 + 10 = 20. The issue is the result is 20 instead of 15. The fix is to add parentheses: (10 + 20) / 2 = 15.
Related Resources
Related Lessons
- Numbers and Math
Learn about numbers and math operations.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.