JavaScript Arithmetic Expressions and Precedence
Quick answer
An expression produces a value when evaluated. Arithmetic operators combine numeric operands. The examples here use small integers to avoid floating-point precision distractions.
How it works
Use +, -, *, / and % for addition, subtraction, multiplication, division and remainder. Multiplication, division and remainder have higher precedence than addition and subtraction. Parentheses express the grouping you intend. For nonnegative integers and a positive divisor, % gives the remainder after division.
console.log(2 + 3 * 4);console.log((2 + 3) * 4);Expected console output (Node.js):
1420Without parentheses multiplication comes first; with them addition comes first.
Expected console output (Node.js):
62Eighteen divides into six groups of three; seventeen leaves two after groups of five.
Common mistakes
% is remainder, not a percentage operator. To compute ten percent of 80, use 80 * 10 / 100. Negative remainder behavior is covered later.
Practice
Calculate the total cost of four items at seven units each plus a delivery fee of three units. Then compute how many items remain after packing 23 into groups of five.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Arithmetic Expressions and Precedence — Knowledge Check
Review the concept and apply it to a new example.
What is the value of 5 + 2 * 3?
What is the value of (5 + 2) * 3?
What does 23 % 5 evaluate to?
Which expression calculates 25 percent of 80?
Which expression calculates four items at seven units each plus a fee of three?