Adding large number in JavaScript

Spread the love

Today i had an Interview with one of the company and interviewer guy (Ritesh) asked me very simple problem that “How will you add two numbers if numbers are very large/Add large number in JavaScript?”. Frankly saying, i never gave a thought about this problem ever. My interviewer didn’t got got impressed but still i thought of solving it. So below id the function that i have coded for it.

Problem:

Javascript support at most 53 bit of integer only. If number goes beyond it, Javascript loose precision.  Small number can be added simply as follow:

But if you try to add large number as following, it will loose accuracy and give results in exponential form.

But we were expecting final output be like 1238788002533331313020201110. For solving this we can write function something like this:



Leave a Reply

Your email address will not be published. Required fields are marked *