Adding big, beautiful, powerful numbers together in JavaScript

What is the result when you add 10000000000000000 + 1? If you answered 10000000000000001, you are wrong in the world of JavaScript. Why? Long story short, JavaScript uses floating point numbers for everything, which means it loses precision when you are dealing with very large or very small numbers.

So, if you really need precision on those large numbers, how would you go about it? One thing JavaScript is much more strict about is how it stores strings and arrays. Unless you change them, they are always going to come back to you the same way every time. It takes about 18 digits before Javascript loses precision on its number, where strings and arrays you are only limited to the amount of memory the interpreter will give you.

Once you have the numbers stored as arrays, you can add them together in the same way they teach primary school kids. Add each digit together and carry the leftover to the next.

Note, this demo only works with positive numbers. Following the same concepts, it'd be trivial to write other operators and dealing with negative numbers. It is beyond the scope of this post.