The expression console.log(0.3 === 0.1 + 0.2);
evaluates to false
in JavaScript due to the way floatingpoint arithmetic is handled in computer systems.
JavaScript, like many other programming languages, uses the IEEE 754 standard for representing floatingpoint numbers. This standard can introduce small precision errors when performing arithmetic operations with floatingpoint numbers. Here's a detailed explanation:

Binary Representation: Floatingpoint numbers are represented in binary, which can lead to precision errors because not all decimal fractions can be exactly represented in binary. For instance, the number 0.1 in binary is a repeating fraction (0.0001100110011...).

Precision Errors: When you add 0.1 and 0.2 in JavaScript, the result is not exactly 0.3 due to these precision errors. Instead, the result is a number very close to 0.3, but not exactly 0.3. Specifically,
0.1 + 0.2
evaluates to something like0.30000000000000004
. 
Strict Equality: The
===
operator in JavaScript checks for strict equality, meaning both the value and the type must be the same without any type conversion. Since0.3
is not exactly equal to0.30000000000000004
, the comparison0.3 === 0.1 + 0.2
evaluates tofalse
.
To illustrate this, you can print the result of 0.1 + 0.2
:
console.log(0.1 + 0.2); // Outputs: 0.30000000000000004
Solution
decimal.js
and BigNumber.js
are popular libraries for arbitraryprecision arithmetic in JavaScript, and each has its own strengths and use cases. Here's a comparison to help you decide which might be better suited for your needs:
BigNumber.js
Pros:
 Popularity: BigNumber.js is widely used and has a large community, which means more community support and resources.
 Performance: Generally faster for basic arithmetic operations.
 API Simplicity: The API is straightforward and easy to use for common tasks.
Cons:
 Precision Control: Limited control over precision and rounding compared to
decimal.js
.
Decimal.js
Pros:
 Precision and Rounding: Offers more extensive control over precision and rounding modes, which can be crucial for financial and scientific applications.
 Compatibility: Better adherence to the ECMAScript proposal for Decimal (though still a proposal, not yet part of the standard).
 Features: Supports a wider range of mathematical operations and functions.
Cons:
 Performance: Slightly slower than BigNumber.js for some operations due to additional precision and rounding control.
 Complexity: More features mean a steeper learning curve for some use cases.
Choosing Between Them
 For Basic Use Cases: If you need arbitraryprecision arithmetic for common tasks and prioritize performance and simplicity,
BigNumber.js
is a solid choice.  For Advanced Use Cases: If your application requires extensive control over precision, rounding, or advanced mathematical functions,
decimal.js
might be more suitable.
Example Usage
BigNumber.js:
// Import the BigNumber library
const BigNumber = require('bignumber.js');
const num1 = new BigNumber(0.1);
const num2 = new BigNumber(0.2);
const sum = num1.plus(num2);
const num3 = new BigNumber(0.3);
console.log(sum.isEqualTo(num3)); // Outputs: true
decimal.js:
// Import the Decimal library
const Decimal = require('decimal.js');
const num1 = new Decimal(0.1);
const num2 = new Decimal(0.2);
const sum = num1.plus(num2);
const num3 = new Decimal(0.3);
console.log(sum.equals(num3)); // Outputs: true
Conclusion
Both libraries are excellent choices for arbitraryprecision arithmetic in JavaScript. Your choice depends on the specific needs of your project:
 Choose BigNumber.js for simplicity and performance.
 Choose decimal.js for precision control and advanced mathematical operations.