Numbers and Math

JavaScript ES2023 · ✓ verified by execution on 2026-08-05

Most languages separate integers from decimals and make you keep track of which is which. JavaScript has one Number type for both. Every number you write — 42, 3.14, a loop counter, a price — is stored the same way: as a double-precision 64-bit float, following the IEEE 754 standard.

One type means no casting and no integer-versus-float bugs. It also means the decimal quirks of floating point apply to every number in the language, including ones that look like plain whole numbers.

Basic Arithmetic

JavaScript supports all the standard arithmetic operators you would expect:

javascript
let sum = 10 + 5;
console.log(sum);
Output
15

The Remainder Operator

The remainder operator (%) returns the amount left over when one number is divided by another. It is often used to check if a number is even or odd.

javascript
console.log(10 % 3);
Output
1

Increment and Decrement

To quickly add or subtract 1 from a variable, you can use the increment (++) and decrement (--) operators.

javascript
let count = 0;
count++;
console.log(count);
Output
1

Absorbing Mathematical Errors

Divide by zero in most languages and you get an exception. Multiply a word by a number and you get another one. JavaScript does neither.

The language was built to run inside a web page, where a fault in one small script should not take the page down with it. So instead of throwing, JavaScript absorbs impossible arithmetic into special numeric values and keeps going.

Infinity

If you try to divide a positive number by zero, JavaScript returns the special value Infinity instead of crashing.

javascript
console.log(10 / 0);
Output
Infinity

Not-A-Number (NaN)

Similarly, when you try to perform a mathematical operation that fundamentally doesn’t make logical sense—such as dividing a word by a number—JavaScript absorbs the error and returns a special value called NaN (Not-A-Number).

javascript
console.log("hello" / 2);
Output
NaN

The NaN Paradox

The name says Not-A-Number, so the obvious guess is that it sits outside the number type — somewhere near undefined. Check it:

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

console.log(typeof NaN);
Output
number

Despite its contradictory name, NaN is technically categorized as a numeric value within JavaScript’s underlying type system! It is best understood as a “corrupted number state”.

NaN spreads

Not crashing has a cost. NaN is contagious: every arithmetic operation touching it produces NaN in turn, so a single bad value silently travels the length of a calculation and surfaces at the end, far from wherever it came from. Worse, NaN is the only value in JavaScript not equal to itself — NaN === NaN is false — so testing for it needs Number.isNaN(x).

NaN Visualization

Here is a simple visualization of how standard numbers, infinity, and NaN all fit harmlessly inside the single JavaScript Number type:

The Number Type Valid Numbers 42, -5, 3.14 Infinity 10 / 0 NaN "a" / 2
The JavaScript Number type encompasses Infinity and NaN

Check yourself

Which arithmetic operator gives the remainder of a division?

Reveal answer

% — The remainder (%) operator returns the remainder left over when one operand is divided by a second operand.

What does the ++ operator do?

Reveal answer

Adds one to its operand — The increment (++) operator increments (adds one to) its operand.

What is the result of typeof NaN?

Reveal answer

"number" — Despite its name, NaN (Not-A-Number) is formally a numeric data type in JavaScript.

Challenges

Challenge 1 +25 XP

Create a variable `total` equal to 50 multiplied by 2. Print it.

javascript
  • Test 1 — expects "100"
Need a hint? (−25% XP)

Use the * operator for multiplication.

Show solution (0 XP)
let total = 50 * 2;
console.log(total);

Challenge 2 +25 XP

Divide the string 'apples' by 2 and print the result, which should be NaN.

javascript
  • Test 1 — expects "NaN"
Need a hint? (−25% XP)

Math on a non-numeric string results in NaN.

Show solution (0 XP)
console.log('apples' / 2);