JavaScript numbers are 64-bit double-precision floats. Base-10 decimals like 0.1 have repeating binary fractions, resulting in 0.30000000000000004. For financial calculations, store integer cents.
Trap #2: NaN !== NaN Self-Equality Trap
NaN is the only value in JavaScript that is not equal to itself: NaN === NaN evaluates to false. Always check with Number.isNaN(val).
Trap #3: BigInt and Number Mixing TypeError
You cannot perform arithmetic between BigInt and Number: 10n + 5 throws TypeError: Cannot mix BigInt and other types. Explicitly cast types before operations.
Trap #4: parseInt() Without Radix 10
Omitting the radix (parseInt("08")) in older engines parsed strings with leading zeros as octal numbers. Always supply the radix: parseInt("42", 10).
Trap #5: Integer Overflow Past Number.MAX_SAFE_INTEGER
Integers greater than 9,007,199,254,740,991 (2^53 - 1) lose precision, causing database IDs (like Twitter Snowflake IDs) to corrupt. Always parse large numeric IDs as strings or BigInt.
💬 Frequently Asked Questions
What is the difference between isNaN() and Number.isNaN()?
The global isNaN(x) coerces x to a number first, making isNaN("hello") true. Number.isNaN(x) does not coerce, returning true only if x is literally NaN.
What is BigInt and when should it be used?
BigInt is an arbitrary-precision integer type used for cryptography, 64-bit timestamps, and massive integer calculations beyond Number.MAX_SAFE_INTEGER.
How does Math.floor() differ from Math.trunc()?
Math.floor() rounds downward toward negative infinity (Math.floor(-1.5) is -2). Math.trunc() truncates fractional digits toward zero (Math.trunc(-1.5) is -1).
How do you format numbers as currency in JavaScript?
Use the modern Intl.NumberFormat API: new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).format(19.99).
What does the unary + operator do to strings?
The unary + operator coerces strings to numbers: +"42" becomes 42, and +"invalid" becomes NaN. It is a compact alternative to Number().