Start with the number type

The examples here work in a modern browser console or a current Node.js environment. JavaScript uses the number type for ordinary numeric values, including integers and decimals. That makes simple arithmetic straightforward: 8 / 2 evaluates to 4, and 8 / 3 produces a fractional result. The convenience has a limit, though: a number cannot represent every possible decimal value or every possible integer exactly.

Try evaluating 0.1 + 0.2. The result is 0.30000000000000004, not exactly 0.3. Most JavaScript numbers use binary floating-point representation, and some decimal fractions do not have an exact representation in that format. This is not a sign that addition is broken. It means the stored inputs are approximations, and a tiny difference can become visible in the result.

Check inputs and invalid results explicitly

An input that looks numeric is not necessarily a number. In a form, a value read from an input element is a string, so "12" + 3 produces "123". Convert deliberately before calculating: Number("12") + 3 produces 15. Number("12px"), however, produces NaN. Check the result rather than assuming conversion succeeded; Number.isNaN(Number("12px")) is true.

NaN represents an invalid numeric result, while Infinity and -Infinity can arise from operations such as dividing a nonzero number by zero. Number.isFinite(value) is useful when a calculation requires an ordinary finite number: it rejects NaN and both infinities. Unlike a broad truthiness check, it does not reject the valid number 0. Decide separately whether zero or negative values make sense for the particular input.

Round for the task, not by habit

Math.round, Math.floor, and Math.ceil answer different questions. For 2.7, they produce 3, 2, and 3 respectively. Negative inputs make their differences clearer: Math.floor(-2.7) is -3, while Math.ceil(-2.7) is -2. Floor moves toward negative infinity; ceil moves toward positive infinity. Neither means “discard the fractional part.” Math.trunc(-2.7) produces -2 when discarding that part is what you intend.

For display, (2.345).toFixed(2) produces the string "2.35". Because it returns a string, do not feed the result back into arithmetic without considering conversion. Decimal values are also stored approximately, so formatting methods may not match an expected decimal-rounding rule in every case. Keep the numeric value for calculations and format it at the point where you present it.

Compare calculated decimals with care

Exact equality remains useful when exact results are expected, but it can be misleading for calculated decimals: 0.1 + 0.2 === 0.3 is false. If the task permits a small difference, compare the distance between results instead. For example, Math.abs((0.1 + 0.2) - 0.3) < 0.000001 is true. The threshold is a choice about acceptable error, not a universal JavaScript constant.

Choose that threshold in the units of your problem. A difference that is negligible for a rough visual position might matter for a precise measurement. Number.EPSILON describes the spacing between 1 and the next representable number above it; using it unchanged for every calculation is not a general solution, especially when values have very different magnitudes. When the required precision is unclear, define it before choosing a comparison.

Know when integers need another approach

JavaScript numbers represent integers exactly only within a limited range. Number.isSafeInteger(value) checks whether a value is an integer in that safe range. For example, Number.isSafeInteger(42) is true. If an application must count beyond that range exactly, BigInt may be appropriate: 9007199254740993n is a BigInt literal. BigInt and number are separate numeric types, so arithmetic between them requires an explicit conversion and a decision about whether that conversion is safe.

A practical routine is to convert inputs intentionally, reject invalid results, keep calculations separate from display formatting, and test boundary cases alongside ordinary examples. To take a break after practicing in the console, browse Music Discovery · Bridge City Music Journal for music discovery.

Back to category ↗