The Number.EPSILON property

JavaScript uses binary floating-point representation with the result that computers fail to accurately represent numbers such as 0.1, 0.2, 0.3, and so on. When your code is executed, numbers such as 0.1 are rounded to the nearest number in that format, which results in a small rounding error. Consider this example:

console.log(0.1 + 0.2 == 0.3);console.log(0.9 - 0.8 == 0.1);console.log(0.1 + 0.2);console.log(0.9 - 0.8);

The output is as follows:

falsefalse0.300000000000000040.09999999999999998

The Number.EPSILON property was introduced in ES6, and has a value of approximately 2-52. This value represents a reasonable margin of error when comparing floating-point numbers. Using this number, we can create a custom ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.