DEV Community

loading...
Cover image for JavaScript Interview #13: Big numbers

JavaScript Interview #13: Big numbers

Coderslang: Become a Software Engineer
Teaching you to code at js.coderslang.com - JavaScript, HTML, CSS, Node.js, React.js, React Native
Originally published at learn.coderslang.com on ・1 min read

js-test-13

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.

Learn Full Stack JavaScript

Discussion (3)

Collapse
jordanaf808 profile image
jordanaf808

I just went through all these and learned something from each of them, thanks!

Collapse
coderslang profile image
Coderslang: Become a Software Engineer Author

Happy to help! You can find more JavaScript interview questions with explanations here

Collapse
yodalightsabr profile image
YodaLightsabr

Yeah. Some of them were like, “Oh, I know this one!” And on others I found something really interesting to take away from it.