JavaScript - Numbers & BigInt

Overview

Estimated time: 30–40 minutes

Numbers are double-precision floating point. BigInt represents arbitrary-precision integers. Learn parsing, formatting, and safe operations.

Learning Objectives

  • Use Number safely with Number.isFinite/Number.isNaN.
  • Parse input with Number, parseInt, parseFloat.
  • Operate with BigInt and understand mixing rules.

Prerequisites

Examples

Number('3.14');      // 3.14
parseInt('101', 2);  // 5
Number.isNaN(NaN);   // true
Number.isFinite(42); // true

(0.1 + 0.2).toFixed(2); // '0.30'

10n + 20n;   // 30n
// 10n + 1;  // TypeError: cannot mix BigInt and Number

Formatting

(1234.567).toFixed(2);      // '1234.57'
(1234.567).toPrecision(4);  // '1235'

Common Pitfalls

  • Floating point rounding errors; round/format results for display.
  • Don't rely on isNaN (global); prefer Number.isNaN.