Skip to the workspace
Pebellslearn by building
Expressions & Operators  ›  Lesson

Arithmetic and Unary Operators

Expressions & Operators 12 min

javascript / Arithmetic and Unary Operators

Explore each scene at your own pace.

01 · Arithmetic and Unary Operators

Arithmetic and Unary Operators

Arithmetic and Unary Operators

Arithmetic operators calculate numeric results. Unary operators act on one operand; some also convert its type.

Keep the calculation separate from how you display its result.

See the mechanism

Walk through the modelArithmetic and Unary OperatorsAuthored snapshots of the example; use Run below to test the code.
articles3
minutesEach4
Both inputs are numbers.
1 of 3

Follow one example

javascript

Multiplication gives 12 reading minutes. % gives the remainder, 1. The last output is approximately 0.30000000000000004 because Number uses binary floating-point; do not assume every decimal fraction has an exact representation. Parentheses make grouping explicit, and * is evaluated before +. Unary - negates a number; unary + converts its operand to Number, so +"4" is numeric 4.

Predict, change and check

Predict: Will (2 + 3) * 4 equal 2 + 3 * 4? Calculate both before running.

Test your explanation: Add both expressions to the example. Then use integer cents for a small money calculation instead of assuming decimal arithmetic is exact.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Numeric operations still have representation rules

Remainder is not universally mathematical modulo for negative values; JavaScript's remainder sign follows the dividend. Exponentiation is different from bitwise XOR. Prefix and postfix increment both update a variable but produce different expression values, so embedding them in complex expressions makes review harder.

BigInt arithmetic is exact integer arithmetic and generally cannot mix Number operands. BigInt division truncates toward zero; division by zero throws instead of returning Number Infinity. Unary plus does not convert BigInt to Number. Use explicit conversions only when the loss of range or precision is acceptable.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.