QA
Intermediate LevelJavaScript & TypeScript

How does JavaScript handle decimal precision issues (0.1 + 0.2 !== 0.3) and BigInt?

❓ Interview Prompt: Explain IEEE 754 binary floating point representation errors, Number.EPSILON, integer safety, and BigInt for large numbers.

JavaScript numbers are double-precision 64-bit binary format IEEE 754 values:

Floating Point Precision Issue:

Fractions like 0.1 cannot be represented exactly in binary floating-point.

•
0.1 + 0.2 evaluates to 0.30000000000000004.
•
Fix equality checks using Math.abs(a - b) < Number.EPSILON.

Large Integers (BigInt):

Standard Number loses integer precision beyond Number.MAX_SAFE_INTEGER (2^53 - 1 = 9007199254740991).

•
Use BigInt (100n or BigInt("9007199254740993")) for arbitrary precision integers.

Code Implementation Example

javascript
// Precision check with Number.EPSILON
function areFloatsEqual(a, b) {
  return Math.abs(a - b) < Number.EPSILON;
}
console.log(0.1 + 0.2 === 0.3); // false ❌
console.log(areFloatsEqual(0.1 + 0.2, 0.3)); // true ✅

// BigInt for large financial/ID numbers
const maxInt = BigInt(Number.MAX_SAFE_INTEGER);
const hugeNumber = maxInt + 2n;
console.log(hugeNumber.toString()); // "9007199254740993"
💡Number.EPSILON for safe floating-point comparison and BigInt for large integers.
🎯 Interview Key Takeaway

Use Number.EPSILON to compare float calculations safely. Use BigInt for integers exceeding Number.MAX_SAFE_INTEGER.

#Floating Point#BigInt#IEEE 754#Intermediate
Back to all JavaScript & TypeScript Questions→

More JavaScript & TypeScript Interview Questions

Beginner LevelJavaScript & TypeScript

What are the differences between var, let, and const in JavaScript?

Beginner LevelJavaScript & TypeScript

What are the JavaScript data types and the difference between primitives and objects?

Beginner LevelJavaScript & TypeScript

What is the difference between == (loose equality) and === (strict equality)?