Why does 0.1 + 0.2 !== 0.3 in JavaScript?
- JavaScript has floating-point precision limitations
- 0.1 and 0.2 are not stored precisely in binary
- It's a bug in JavaScript
- JavaScript uses a different numeric system
In JavaScript, numbers are represented in binary, and not all decimal fractions can be precisely represented in binary. Due to this limitation, when you perform operations like addition with decimal numbers, you might encounter tiny rounding errors that make 0.1 + 0.2 !== 0.3. This behavior is not unique to JavaScript and occurs in many programming languages with floating-point arithmetic.
Loading...
Related Quiz
- While working on a project, you found out that the API you are fetching data from sends relevant error messages in the body of the response, even when the request fails. How do you extract and use this error message in JavaScript?
- The switch statement evaluates expressions based on their _________.
- In a do-while loop, when is the condition checked?
- Why might for...of loops be preferred when working with asynchronous code?
- In order to make an object iterable with a for...of loop, you need to define its _______ method.