Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Expressions & Operators  ›  Lesson

Arithmetic and Unary Operators

Arithmetic Operators 12 minArithmetic operators (+ - * / % **) combine two values; unary operators (+ - ++ --) act on one, converting to a number, flipping its sign, or stepping it by one; BigInt arithmetic only mixes with other BigInts.

Arithmetic and Unary Operators

In short: the six arithmetic operators — + - * / % ** — combine two values: add, subtract, multiply, divide, remainder, and raise to a power. Unary operators act on a single value: +x converts x to a number, -x flips its sign, and ++/-- step a number by one, either before or after the expression's value is read. BigInt values (written 10n) have their own arithmetic and refuse to mix with ordinary numbers — you must convert one side explicitly.

A keypad with + − * / ** keys and a green % key (7 % 3 → 1, 2 ** 10 → 1024), a unary lever box (+"42" → 42, -"42" → −42), and an n++ vs ++n comparison — plus a note that BigInt only does arithmetic with another BigInt

Split the remainder from a total

% gives the remainder after division — useful for anything that wraps, like converting seconds into minutes and leftover seconds:

javascript

7 % 3 is 1 — 3 goes into 7 twice, with 1 left over. 2 ** 10 is 1024 — ** is JavaScript's power operator. / always gives a full decimal, 3.3333333333333335 — JavaScript has one number type, there's no separate "integer division".

Convert a form value with unary plus

The unary + in front of a single value is a quick way to force a string into a number — it's the same conversion Number() does, just shorter:

javascript

+raw is the number 42; typeof +raw confirms it's "number", not "string". -raw does the same conversion and then negates it: -42. Contrast this with binary +, which needs two values: 5 + "5" joins text and gives "55", because one side is a string. The difference between unary and binary + is entirely about how many values sit around it.

Read n++ before and after it runs

n++ (postfix) returns the old value and updates n afterwards; ++n (prefix) updates n first and returns the new value:

javascript

The first line prints 5 6 — n++ hands back 5 (its value before incrementing), and only then does n become 6. The second prints 7 7 — ++n increments first, so both the returned value and n itself are 7. Mixing these up inside a larger expression is a classic source of off-by-one bugs.

The trap: mixing BigInt with a regular number

BigInt exists for integers too large for the normal number type to represent exactly. It refuses to silently combine with ordinary numbers:

javascript

This throws TypeError: Cannot mix BigInt and other types, use explicit conversions. Both sides must be the same kind: 10n + 5n gives 15n, or convert explicitly with Number(10n) + 5 (losing BigInt's extra range) or 10n + BigInt(5).

Quick practice

quick drill

What is 7 % 3?

quick drill

What is typeof +"42"?

quick drill

What does let n = 5; console.log(n++); print, and what is n afterwards?

quick drill

What does 10n + 5 do?

quick drill

What is 2 ** 10?

🎯 Your turn

Explain the difference between +"5" and 5 + "5", what each evaluates to, and why 10n + 5 fails while 10n + 5n works.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTwo different pluses

Explain the difference between +"5" and 5 + "5", what each evaluates to, and why 10n + 5 fails while 10n + 5n works.