Friday, October 10, 2025
HomeProgrammingSum an Array of Numbers with JavaScript

Sum an Array of Numbers with JavaScript


It is uncommon that I am dissatisfied by the JavaScript language not having a operate that I want. One such case was summing an array of numbers — I used to be anticipating Math.sum or a likewise, baked in API. Concern not — summing an array of numbers is simple utilizing Array.prototype.scale back!

const numbers = [1, 2, 3, 4];
const sum = numbers.scale back((a, b) => a + b, 0);

The 0 represents the beginning worth whereas with a and b, one represents the operating complete with the opposite representing the worth to be added. You may additionally word that utilizing scale back prevents unintended effects! I would nonetheless favor one thing like Math.sum(...numbers) however a easy scale back will do!

  • CSS Animations Between Media Queries

    CSS animations are proper up there with sliced bread. CSS animations are environment friendly as a result of they are often {hardware} accelerated, they require no JavaScript overhead, and they’re composed of little or no CSS code. Very often we add CSS transforms to components by way of CSS throughout…

  • How I Stopped WordPress Comment Spam

    I really like virtually each a part of being a tech blogger:  studying, preaching, bantering, researching.  The one half about running a blog that I completely detest:  coping with SPAM feedback.  For the previous two years, my weblog has registered 8,000+ SPAM feedback per day.  PER DAY.  Bloating my database…


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments