# Numbers, BigInt & Math

> Explore how JavaScript handles numbers, why 0.1 + 0.2 is broken, and when to use BigInt for massive integers.

Subject: JavaScript | Lesson 6 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/numbers-and-math
Previous: [Strings & Template Literals](https://codewithanimation.in/javascript/strings-and-template-literals.md) | Next: [Type Conversion (Explicit)](https://codewithanimation.in/javascript/type-conversion.md)

## The Number Type

Unlike many languages that have separate types for integers (`int`) and decimals (`float`), JavaScript historically only had one: `Number`.

Every `Number` in JavaScript is a 64-bit floating-point value (IEEE 754). This means there is no true 'integer' type under the hood; even `42` is technically stored as `42.0`.

## The floating point trap

Because computers operate in binary (base-2), they struggle to cleanly represent certain base-10 decimals, much like how `1/3` cannot be written cleanly in decimal (0.3333...).

This leads to the famous `0.1 + 0.2 === 0.30000000000000004` bug. When dealing with currency or precise measurements, you must account for this floating-point math rounding error.

## Math and BigInt

JavaScript provides a built-in `Math` object loaded with helpful static methods like `Math.round()`, `Math.floor()`, `Math.max()`, and constants like `Math.PI`.

Because the 64-bit `Number` uses some bits for decimals, its maximum safe integer is `9007199254740991` (`Number.MAX_SAFE_INTEGER`).

To represent numbers larger than this, JavaScript introduced `BigInt`. You create one by appending an `n` to the end of a literal, like `999999999999999999n`. `BigInt` can be infinitely large, but it cannot contain decimals.

## Common mistake: Mixing BigInt and Number

You cannot mix `BigInt` and `Number` in a single mathematical operation. For example, `10n + 5` will throw a TypeError. You must explicitly convert one type to match the other first: `10n + BigInt(5)`.

## Tip: Fixing decimal math

If you are working with money (where precision is critical), multiply your decimals to turn them into integers before doing math. For example, `$10.50` becomes `1050` cents. Do the math in cents, then divide by 100 before displaying.

## Try it: The Floating Point Flaw

Check out what happens when you try to do basic decimal math in JavaScript, and see how the `BigInt` type avoids the maximum safe integer limit.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/numbers-and-math

## Check yourself

### Question 1

What is the result of `0.1 + 0.2 === 0.3`?

- A. false
- B. true
- C. NaN

Correct answer: A

Because JavaScript uses binary floating-point numbers, decimals like 0.1 and 0.2 cannot be perfectly represented, causing rounding errors in base-10 math.

- Why not B: Incorrect. Due to floating point imprecision, the left side evaluates to 0.30000000000000004.
- Why not C: Incorrect. The math completes, it is just slightly inaccurate.

### Question 2

How do you define a BigInt literal?

- A. By appending an 'n' to the end of the number (e.g. 100n)
- B. By using the new keyword (e.g. new BigInt(100))
- C. By adding a 'b' to the end of the number (e.g. 100b)

Correct answer: A

You can create a BigInt either by appending an 'n' to an integer literal (10n) or by calling the BigInt() function without the new keyword.

- Why not B: Incorrect. BigInt is not a constructor and cannot be used with `new`.
- Why not C: Incorrect. The syntax uses an 'n'.

### Question 3

Can a BigInt hold a decimal value (e.g., 5.5n)?

- A. No, BigInts are strictly for whole integers.
- B. Yes, it just uses more memory than a standard Number.
- C. Only if the decimal is exactly .5

Correct answer: A

BigInt stands for Big Integer. It does not support fractional values. If you do division with a BigInt, it will truncate the remainder towards zero.

- Why not B: Incorrect. BigInt is specifically an integer type.
- Why not C: Incorrect. BigInt does not support fractional values.

## Remember this

- JavaScript standard Numbers are 64-bit floating point values (IEEE 754).
- Decimal math in JavaScript can be imprecise (e.g., `0.1 + 0.2`).
- `BigInt` can store integers larger than `Number.MAX_SAFE_INTEGER` but cannot hold decimals.
- You cannot directly mix `Number` and `BigInt` in mathematical operations.
