Skip to content
  • Numbers, Math & BigInt · JavaScript

Numbers, Math & BigInt

LearnViaNotes @learnvianotes· 2 min read

One number type for integers and decimals — a 64-bit IEEE 754 double — plus BigInt for arbitrarily large integers.

Floating-point reality

JavaScript
console.log(0.1 + 0.2);
console.log(0.1 + 0.2 === 0.3);
console.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON);
console.log((0.1 + 0.2).toFixed(2), Math.round(1.005 * 100) / 100);
Output
0.30000000000000004
false
true
0.30 1

Special values

JavaScript
console.log(1 / 0, -1 / 0, 0 / 0);
console.log(typeof NaN, NaN === NaN);
console.log(isNaN("abc"), Number.isNaN("abc"));
console.log(Number.isFinite("5"), isFinite("5"));
console.log(Number.MAX_SAFE_INTEGER, Number.isSafeInteger(2 ** 53));
console.log(2 ** 53 === 2 ** 53 + 1);
Output
Infinity -Infinity NaN
number false
true false
false true
9007199254740991 false
true
Global (coerces)Number.* (no coercion) — prefer these
isNaN("abc") → trueNumber.isNaN("abc") → false
isFinite("5") → trueNumber.isFinite("5") → false
parseInt, parseFloatNumber.parseInt, Number.parseFloat (identical)

Formatting and parsing

JavaScript
const n = 1234.5678;
console.log(n.toFixed(2), n.toPrecision(3), (255).toString(16), (5).toString(2));
console.log(Number("1_000"), 1_000_000, 0xff, 0b1010, 0o17, 1e3);
console.log((1234567.891).toLocaleString("en-US"));
console.log((1234567.891).toLocaleString("de-DE"));
Output
1234.57 1.23e+3 ff 101
NaN 1000000 255 10 15 1000
1,234,567.891
1.234.567,891

The Math object

MethodResult
Math.round(2.5), Math.round(-2.5)3, -2 (rounds half toward +∞)
Math.floor(-1.5), Math.ceil(-1.5), Math.trunc(-1.5)-2, -1, -1
Math.max(...arr), Math.min()largest; Math.min() with no args is Infinity
Math.abs, Math.sign, Math.sqrt, Math.cbrt, Math.hypotthe usual
Math.random()float in [0, 1) — not cryptographically secure
JavaScript
console.log(Math.round(2.5), Math.round(-2.5), Math.trunc(-1.7), Math.floor(-1.7));
console.log(Math.max(3, 9, 1), Math.max(...[4, 8]), Math.max(), Math.min());
console.log(Math.hypot(3, 4), 2 ** 10, 7 % 3, -7 % 3);
const randInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const r = randInt(1, 6);
console.log(r >= 1 && r <= 6 && Number.isInteger(r));
Output
3 -2 -1 -2
9 8 -Infinity Infinity
5 1024 1 -1
true

BigInt

JavaScript
const big = 2n ** 64n;
console.log(big, typeof big);
console.log(9007199254740993n + 1n, 9007199254740993 + 1);
console.log(7n / 2n, BigInt("123") * 2n, 5n == 5, 5n === 5);
try {
  console.log(1n + 1);
} catch (e) {
  console.log(e.name + ": " + e.message);
}
Output
18446744073709551616n bigint
9007199254740994n 9007199254740992
3n 246n true false
TypeError: Cannot mix BigInt and other types, use explicit conversions
  • BigInt division truncates (7n / 2n === 3n); no decimals.

  • Cannot mix with number in arithmetic — convert with BigInt(n) or Number(b).

  • Not usable with Math.*; JSON.stringify throws on BigInt unless you add a toJSON.

Why is 0.1 + 0.2 !== 0.3?

What is Number.MAX_SAFE_INTEGER?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report