Skip to main content

Sum numbers in an array with JavaScript

· 6 min read
Filip Tammergård
Software Engineer at Frilans Finans

There are several ways to compute the sum of numbers in an array with JavaScript. The approaches differ mainly in how readable they are — and readable code is easier to maintain and easier for others to understand and build on.

TL;DR

  • There are many ways to sum numbers in an array, but several of them are hard to read and unnecessarily complicated.
  • The best way is using the built-in reduce function — or the newer Math.sumPrecise if you can rely on it being available.