We’re just logging a number, what can go wrong here?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Under the hood, there are no integers in JavaScript.
All numbers are represented as 64-bit
floats. This is also known as double precision
.
52 bits
are used to store digits, 11 bits
serve to track the position of the decimal point, and 1 bit
holds the sign and determines whether the number is positive or negative.
When there’s not enough "space" to store the whole number, then rounding to the nearest possible integer occurs.
It’s impossible to store the number 9999999999999999
using 52 bits
, so the rounding gets rid of the least significant digits which leads to the result of 10000000000000000
.
In JavaScript, no error will be thrown in this case.
If you haven’t quite understood what’s going on here, make sure to read the lecture on Binary Number System of my Full Stack JS course CoderslangJS.
ANSWER: 10000000000000000
will be printed to the screen.
Top comments (3)
I just went through all these and learned something from each of them, thanks!
Happy to help! You can find more JavaScript interview questions with explanations here
Yeah. Some of them were like, “Oh, I know this one!” And on others I found something really interesting to take away from it.