JavaScript Interview Question #32: 0.1 + 0.1 + 0.1 === 0.3

What will be logged to the console?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

At a first glance, the answer is true as 0.1 + 0.1 + 0.1 is obviously equal to 0.3 .

But that’s only before we get into the details of how the numbers are rep…


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer

js-test-32

What will be logged to the console?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

At a first glance, the answer is true as 0.1 + 0.1 + 0.1 is obviously equal to 0.3 .

But that’s only before we get into the details of how the numbers are represented in JavaScript.

If you try to execute the statement console.log(0.1 + 0.2) in JS, you’ll get a number 0.30000000000000004.

This happens because in JavaScript and quite a few other programming languages some decimal numbers can't be represented exactly as they are.

For example 0.1 in binary will result in an endless fraction, the same way as 1/3 becomes 0.333(3) in the decimal number system.

ANSWER: false will be logged to the console.

Learn Full-Stack JavaScript


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer


Print Share Comment Cite Upload Translate Updates
APA

Coderslang: Become a Software Engineer | Sciencx (2021-04-14T14:47:43+00:00) JavaScript Interview Question #32: 0.1 + 0.1 + 0.1 === 0.3. Retrieved from https://www.scien.cx/2021/04/14/javascript-interview-question-32-0-1-0-1-0-1-0-3/

MLA
" » JavaScript Interview Question #32: 0.1 + 0.1 + 0.1 === 0.3." Coderslang: Become a Software Engineer | Sciencx - Wednesday April 14, 2021, https://www.scien.cx/2021/04/14/javascript-interview-question-32-0-1-0-1-0-1-0-3/
HARVARD
Coderslang: Become a Software Engineer | Sciencx Wednesday April 14, 2021 » JavaScript Interview Question #32: 0.1 + 0.1 + 0.1 === 0.3., viewed ,<https://www.scien.cx/2021/04/14/javascript-interview-question-32-0-1-0-1-0-1-0-3/>
VANCOUVER
Coderslang: Become a Software Engineer | Sciencx - » JavaScript Interview Question #32: 0.1 + 0.1 + 0.1 === 0.3. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/14/javascript-interview-question-32-0-1-0-1-0-1-0-3/
CHICAGO
" » JavaScript Interview Question #32: 0.1 + 0.1 + 0.1 === 0.3." Coderslang: Become a Software Engineer | Sciencx - Accessed . https://www.scien.cx/2021/04/14/javascript-interview-question-32-0-1-0-1-0-1-0-3/
IEEE
" » JavaScript Interview Question #32: 0.1 + 0.1 + 0.1 === 0.3." Coderslang: Become a Software Engineer | Sciencx [Online]. Available: https://www.scien.cx/2021/04/14/javascript-interview-question-32-0-1-0-1-0-1-0-3/. [Accessed: ]
rf:citation
» JavaScript Interview Question #32: 0.1 + 0.1 + 0.1 === 0.3 | Coderslang: Become a Software Engineer | Sciencx | https://www.scien.cx/2021/04/14/javascript-interview-question-32-0-1-0-1-0-1-0-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.