Numbers, Precision and BigInt
Build a precise mental model of JavaScript values, scope, functions, objects, arrays and asynchronous behavior, then implement and debug real utilities from first principles instead of memorizing outputs.
What you will be able to do
#- Explain why floating point arithmetic is inexact and compare safely.
- Choose between Number, integer cents and BigInt for a given value.
- Use Number.isInteger, Number.isFinite and Number.isNaN without coercion surprises.
Overview
#One numeric type
Everyday JavaScript arithmetic uses the 64-bit IEEE 754 double. Integers are exact only up to Number.MAX_SAFE_INTEGER, which is 2^53 - 1. Past that boundary adding one can produce the same value, so large identifiers and counters belong in BigInt.
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2); // true
Floating point is not a bug
Doubles cannot represent every decimal fraction exactly. 0.1 and 0.2 are stored as the nearest binary fractions, so their sum is not exactly 0.3. Compare with a tolerance instead of equality.
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false
console.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON); // true
Checking and converting
Use Number.isInteger, Number.isFinite and Number.isNaN rather than the global versions, which coerce their argument. parseInt needs a radix and stops at the first invalid character, while Number() parses the whole string and returns NaN on failure. For money, store integer cents and format at the edge to avoid rounding drift.
BigInt
BigInt literals end in n and support arbitrary precision, but you cannot mix BigInt and Number in one operation without an explicit conversion.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What does this line print?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.javascriptconsole.log(0.1 + 0.2 === 0.3, Number.isInteger(1.0), Number.isNaN('x'), typeof NaN);Exercise 2Explain
Explain why 0.1 + 0.2 is not exactly 0.3 and how you should compare the result.
starter.textconsole.log(0.1 + 0.2);Reveal the reference answer
reference.textDoubles store numbers as binary fractions and cannot represent 0.1 or 0.2 exactly, so each is already slightly off before the addition and the sum is 0.30000000000000004. To compare, check that the absolute difference is below a small tolerance such as Number.EPSILON, or convert to integers before comparing.Authored expected output: 0.30000000000000004
The mismatch comes from binary representation, not from a broken engine. Equality on floats is unreliable, so a tolerance or integer arithmetic is the fix.
Exercise 3Implement
Implement `roundToCents(value)` so it returns the value in integer cents. Running `console.log(roundToCents(19.99), roundToCents(0.1 + 0.2))` should print `1999 30`.
starter.javascriptfunction roundToCents(value) { // your code here }Reveal the reference answer
reference.javascriptfunction roundToCents(value) { return Math.round(value * 100); }Authored expected output: 1999 30
Multiplying by 100 shifts the value to cents and Math.round removes the floating point error, so 0.30000000000000004 becomes 30.
Exercise 4Debug
`toNumber` should convert a numeric string to a number and reject invalid input, but it accepts '42abc'. Find and fix the bug.
starter.javascriptfunction toNumber(value) { return parseInt(value); } console.log(toNumber('42'), toNumber('42abc'));Reveal the reference answer
reference.javascriptfunction toNumber(value) { const parsed = Number(value); return Number.isFinite(parsed) ? parsed : NaN; } console.log(toNumber('42'), toNumber('42abc'));Authored expected output: 42 NaN
parseInt reads digits from the start and ignores trailing characters, so '42abc' becomes 42. Number() parses the whole string and returns NaN for '42abc'.
Exercise 5Test
Write assertions that prove Number.isNaN does not coerce while the global isNaN does.
starter.javascriptfunction assertEqual(actual, expected, label) { if (!Object.is(actual, expected)) throw new Error(label + ': expected ' + expected + ' but got ' + actual); }Reveal the reference answer
reference.javascriptassertEqual(Number.isNaN('x'), false, 'Number.isNaN does not coerce'); assertEqual(isNaN('x'), true, 'global isNaN coerces'); assertEqual(Number.isNaN(NaN), true, 'NaN is NaN'); console.log('all assertions passed');Authored expected output: all assertions passed
The global isNaN converts its argument to a number first, so 'x' becomes NaN and the check is true. Number.isNaN only returns true for the actual NaN value.
Exercise 6Interview
How would you store and calculate money in a JavaScript application?
Reveal the reference answer
reference.textI store amounts as integer cents, or as a decimal string when the value crosses a system boundary, and only format to a currency string at the edge with Intl.NumberFormat. I never add floats directly, because repeated rounding drifts. For very large or high-precision values I use BigInt or a decimal library, and I keep one rounding rule for the whole codebase.The answer should name integer or string storage, a single rounding policy and formatting at the edge, and should acknowledge that floats are unsafe for money.
Common misconceptions
#- 0.1 + 0.2 failing to equal 0.3 is an engine bug rather than a property of binary floating point.
- parseInt and Number convert strings the same way.
- The global isNaN is a safe way to test for the NaN value.
Follow-up practice
#- javascript-foundations-dates-and-time
- javascript-foundations-arrays-and-iteration
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.