Dataslope logoDataslope

Numbers and Arithmetic

How JavaScript represents numbers, the operators you use on them, and the surprising things floating-point arithmetic does.

Numbers are the most universal kind of value. We will spend this page learning what JavaScript means by "a number", what you can do with them, and the small set of gotchas worth knowing in advance.

All numbers are floating-point

Most programming languages have two numeric types: integers (int) and decimals (float). JavaScript, for historical simplicity, has just one:

Every JavaScript number is a 64-bit floating-point value (specifically, IEEE 754 double precision).

That means 7, 7.0, and 7.0000 are all the same value, the same type, stored the same way. There is no distinction between "integer" and "decimal" at the type level.

Code Block
JavaScript ES2023+

(There is a separate, very advanced type called bigint for arbitrarily large integers, written like 42n. You can safely ignore it for now.)

Basic arithmetic

The familiar operators all work as expected:

OperatorMeaningExampleResult
+Add2 + 35
-Subtract2 - 3-1
*Multiply2 * 36
/Divide7 / 23.5
%Remainder (modulo)7 % 31
**Exponent2 ** 101024

Unary operators:

  • -x — negate.
  • +x — coerce to number (mostly useful when x is a string).
Code Block
JavaScript ES2023+

Order of operations

JavaScript uses ordinary mathematical precedence. *, /, %, ** bind more tightly than + and -. Use parentheses to make intent obvious.

Code Block
JavaScript ES2023+

When in doubt, parenthesise. There is no prize for the shortest expression.

Compound assignment

When you want to update a variable based on its current value, you can use the compact compound-assignment operators:

OperatorEquivalent toExample
+=x = x + ...x += 5
-=x = x - ...x -= 5
*=x = x * ...x *= 5
/=x = x / ...x /= 5
%=x = x % ...x %= 5
**=x = x ** ...x **= 2

Also: x++ is x = x + 1 (and returns the old value), x-- is x = x - 1. These are common in loops; we will see them often.

Code Block
JavaScript ES2023+

Special numeric values

JavaScript has three special numeric values you will eventually encounter:

  • Infinity — what you get when you divide a positive number by zero, or when a number gets too large to represent.
  • -Infinity — same, the negative side.
  • NaN — "Not a Number". The result of mathematically undefined operations like 0 / 0 or Math.sqrt(-1), or of trying to convert a non-numeric string to a number.
Code Block
JavaScript ES2023+

The NaN === NaN trap is one of the most famous quirks in JavaScript. To check for NaN, use Number.isNaN(value). Never use value === NaN.

Floating-point surprises

Because all JavaScript numbers are stored as 64-bit floating-point, some calculations that look exact actually have tiny rounding errors. The classic example:

Code Block
JavaScript ES2023+

This is not a JavaScript bug — it is how floating-point arithmetic works in essentially every language (C, Java, Python, Rust, …). The number 0.1 cannot be represented exactly in binary, any more than 1/3 can be represented exactly in decimal.

What to do about it:

  • For everyday code, this rarely matters. Display rounded values ((0.1 + 0.2).toFixed(2)"0.30").
  • When comparing decimals, compare with a small tolerance: Math.abs(a - b) < 1e-9.
  • For money, never use floating point if you can help it. Store amounts as integer cents (a $1.23 price becomes 123), do all arithmetic on integers, and only convert to dollars-with-decimals for display. This is what real e-commerce systems do.

Useful number tools

A few built-ins you will use constantly:

Code Block
JavaScript ES2023+

Notice the very important detail: toFixed returns a string, not a number. This is a recurring beginner trap. If you write x.toFixed(2) + 1, you get the string "1234.571", not the number 1235.57. When you want to display a value, use toFixed. When you want to compute with a value, keep it as a number.

Integer-safe range

Because of floating point, JavaScript can represent integers exactly only up to 2 ** 53 - 1 (9007199254740991). Beyond that, you lose precision.

Code Block
JavaScript ES2023+

This rarely matters for everyday code. It does matter if you handle truly large numeric IDs (some databases produce them) or do heavy mathematics.

Putting it together: a small calculator

Below is a tiny "calculator" that exercises most of what we have learned. Try changing the inputs and see what happens.

Code Block
JavaScript ES2023+

Challenge

Challenge
JavaScript ES2023+
Temperature conversion

Write two functions:

  • toCelsius(f) — converts Fahrenheit to Celsius using (f - 32) * 5 / 9.
  • toFahrenheit(c) — converts Celsius to Fahrenheit using c * 9 / 5 + 32.

Both should return a number, not a string. The tests use a small tolerance to allow for floating-point rounding.


QuestionSelect one

Why does 0.1 + 0.2 === 0.3 evaluate to false in JavaScript?

Because JavaScript has a bug in its addition operator

Because JavaScript numbers are 64-bit floating-point, and decimals like 0.1 cannot be represented exactly in binary — tiny rounding errors are unavoidable

Because 0.3 is actually null under the hood

Because === always returns false for decimals

On this page