Get ready to try the examples

You can try every expression in this article in the console of a modern browser’s developer tools. The examples use JavaScript features available in ECMAScript 2016 and later; they need no library or project setup. Type an expression, press Enter, and inspect the result. A console may display a result differently from a web page, but the arithmetic rules are the same.

An operator tells JavaScript what calculation to perform. In 8 + 3, the numbers are operands and + is the addition operator. You can also use variables: if you enter let price = 8; followed by price + 3, the second expression evaluates to 11. Throughout this guide, the examples use ordinary JavaScript numbers rather than BigInt values.

Meet the arithmetic operators

The familiar operators are + for addition, - for subtraction, * for multiplication, and / for division. For example, 8 + 3 evaluates to 11, while 8 - 3 evaluates to 5. The expressions 8 * 3 and 8 / 2 evaluate to 24 and 4. JavaScript uses * rather than the multiplication symbol you might write on paper.

The % operator gives the remainder after division. For example, 10 % 3 evaluates to 1 because 3 fits into 10 three times with 1 left over. It is useful for tasks such as checking whether a whole number is even: 12 % 2 evaluates to 0. With negative numbers, the result can be negative, so do not assume % always produces a positive value.

The ** operator raises a number to a power: 2 ** 3 evaluates to 8. Division can produce a fractional result, as in 7 / 2, which evaluates to 3.5. It does not automatically round down to a whole number.

Follow the order of operations

When an expression contains several operators, JavaScript does not simply work from left to right. Exponentiation has higher precedence than multiplication, division, and remainder; those have higher precedence than addition and subtraction. Thus 2 + 3 * 4 evaluates to 14, not 20. JavaScript multiplies 3 by 4 before adding 2.

Operators at the same precedence level generally group from left to right. For example, 20 / 5 * 2 evaluates to 8: first 20 / 5 gives 4, then 4 * 2 gives 8. Subtraction behaves similarly, so 10 - 3 - 2 evaluates to 5. Exponentiation is an exception: it groups from right to left. The expression 2 ** 3 ** 2 means 2 ** (3 ** 2), which evaluates to 512.

Parentheses let you choose a different grouping and make your intention clear. The expression (2 + 3) * 4 evaluates to 20 because the addition happens first. Likewise, 20 / (5 * 2) evaluates to 2 rather than 8. If a reader might need to pause and reconstruct the precedence rules, adding parentheses is usually worthwhile even when they do not change the result.

Watch for values that change the calculation

The + operator also joins strings. If a value is text, the result may not be the addition you expect: 8 + "3" produces "83". By contrast, 8 - "3" produces 5 because subtraction does not join strings. This difference is easy to encounter with values entered into a form, which commonly arrive as text. If you intend to calculate, convert and check the input first; for example, Number("3") produces the number 3.

JavaScript numbers also have limits. Calculations with decimal fractions may not match an exact decimal calculation: 0.1 + 0.2 is not exactly 0.3 in JavaScript’s usual number format. For comparisons involving calculated decimals, consider whether a small difference is acceptable instead of assuming exact equality. Dividing a nonzero number by zero produces Infinity rather than an ordinary finite result, so validate a divisor when zero is possible.

Build and check a small calculation

Suppose three notebooks cost 4 each and delivery costs 2. The expression 3 * 4 + 2 evaluates to 14. If delivery is charged for each notebook instead, 3 * (4 + 2) evaluates to 18. Both expressions are valid; the parentheses show which cost model you mean.

When a result looks wrong, break the expression into smaller pieces in the console. Evaluate 3 * 4 and 4 + 2 separately, then compare those results with the full expressions. Check whether your values are numbers or strings, and add parentheses to make each intended step explicit. This habit is often more useful than memorizing every precedence rule: it helps you explain the calculation to someone else and spot mistakes before using it in a larger program.

Back to category ↗