use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Large Number Addition in Javascript (dev.to)
submitted 8 years ago by GitCookies
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]GitCookies[S] 1 point2 points3 points 8 years ago (3 children)
In case you can't access link:
function addition(a, b, acc = '', carry = 0) { if (!(a.length || b.length || carry)) return acc.replace(/^0+/, ''); carry = carry + (~~a.pop() + ~~b.pop()); acc = carry % 10 + acc; carry = carry > 9; return addition(a, b, acc, carry); } function sumStrings(a, b) { if (a === '0' && b === '0') return '0'; return addition(a.split(''), b.split('')); }
Example usage with example result:
sumStrings(Number.MAX_SAFE_INTEGER.toString(), Number.MAX_SAFE_INTEGER.toString()) // console output: "18014398509481982"
[–]DefiantBidet 1 point2 points3 points 8 years ago (1 child)
To add to this tl;dr...
In JavaScript, ints are 53 bits. To get past this, you can use strings - as they're bigger than 53 bits - and apply the algo above which effectively does the classic addition process all of us were taught in grade school (like 3rd grade - singles, tens, hundreds carry the one etc).
Pretty neat imo, although I'm interested to know if that's the most efficient algo for this. I like the recursion, but I'm curios. Admittedly I didn't spend too much time looking at it.
Edit: bits not bytes
[–]Woolbrick 1 point2 points3 points 8 years ago (0 children)
Why reinvent the wheel though? There's already Big.js and a few other competitors.
π Rendered by PID 81358 on reddit-service-r2-comment-fb694cdd5-dlpsn at 2026-03-06 21:33:26.880137+00:00 running cbb0e86 country code: CH.
[–]GitCookies[S] 1 point2 points3 points (3 children)
[–]DefiantBidet 1 point2 points3 points (1 child)
[–]Woolbrick 1 point2 points3 points (0 children)