How do you sum an array in javascript?
This'd be exactly the job for Show
If you're using ECMAScript 2015 (aka ECMAScript 6):
For older JS:
Isn't that pretty? :-)
Ry-♦ 211k54 gold badges441 silver badges455 bronze badges answered May 25, 2013 at 16:30
Florian MargaineFlorian Margaine 55.7k14 gold badges90 silver badges116 bronze badges 5 Recommended (reduce with default value)Array.prototype.reduce can be used to iterate through the array, adding the current element value to the sum of the previous element values.
Without default valueYou get a TypeError
Prior to ES6's arrow functions
Non-number inputsIf non-numbers are possible inputs, you may want to handle that?
Non-recommended dangerous eval useWe can use eval to execute a string representation of JavaScript code. Using the Array.prototype.join function to convert the array to a string, we change [1,2,3] into "1+2+3", which evaluates to 6.
Of course displaying an alert isn't the worst thing that could happen. The only reason I have included this is as an answer Ortund's question as I do not think it was clarified.
answered Apr 12, 2017 at 7:31
OwChallieOwChallie 13.5k1 gold badge9 silver badges11 bronze badges 2
Why not reduce? It's usually a bit counter intuitive, but using it to find a sum is pretty straightforward:
answered Jan 16, 2012 at 9:45
Casey RodarmorCasey Rodarmor 14.2k5 gold badges29 silver badges32 bronze badges 10
Kartikey 3,9444 gold badges13 silver badges36 bronze badges answered Aug 4, 2009 at 22:31
AmberAmber 486k81 gold badges615 silver badges545 bronze badges 10
answered Aug 4, 2009 at 22:30
Tyler CarterTyler Carter 59.7k20 gold badges126 silver badges148 bronze badges 8 Anyone looking for a functional oneliner like me? Assuming:
Here's the oneliner for modern JS:
(If you happen to have to support ye olde IE without arrow functions:)
Note that 0 is the initial value here, so you can use that as offset if needed. Also note that this initial value is needed, otherwise calling the function with an empty array will error. answered Feb 2, 2015 at 17:54
geek-merlingeek-merlin 1,42216 silver badges12 bronze badges 0 If you happen to be using Lodash you can use the sum function
simhumileco 28.2k16 gold badges126 silver badges106 bronze badges answered Jan 7, 2016 at 14:26
4 This is possible by looping over all
items, and adding them on each iteration to a
JavaScript doesn't know block scoping, so
The same as above, however annotated and prepared as a simple function:
answered May 25, 2013 at 16:03
yckartyckart 30.8k9 gold badges117 silver badges127 bronze badges 3
Reference: Array.prototype.reduce()
answered Dec 13, 2014 at 7:49
onhoutonhout 2873 silver badges3 bronze badges 2 OK, imagine you have this array below:
Let's start looking into many different ways to do it as I couldn't find any comprehensive answer here: 1) Using built-in reduce()
2) Using for loop
3) Using while loop
4) Using array forEach
and call it like this:
It's not recommended to prototype something like this to Array... answered Dec 31, 2017 at 17:38
AlirezaAlireza 95.3k26 gold badges262 silver badges166 bronze badges Funny approach:
answered Oct 20, 2016 at 15:17
electronelectron 7637 silver badges17 bronze badges 5 A standard JavaScript solution:
This works for me (the result should be 5). I hope there is no hidden disadvantage in this kind of solution.
answered Sep 1, 2012 at 14:23
nanonano 1271 silver badge2 bronze badges 3 I am a beginner with JavaScript and coding in general, but I found that a simple and easy way to sum the numbers in an array is like this:
Basically, I wanted to contribute this because I didn't see many solutions that don't use built-in functions, and this method is easy to write and understand.
answered Apr 19, 2016 at 16:46
1 You can try the following code:
answered Apr 7 at 0:21
Use a
Or even a
For simplicity, use
Hunter 2,69819 silver badges22 bronze badges answered Aug 22, 2019 at 10:04 A few people have suggested adding a If you still insist on doing it then this is a succinct way of writing it:
then: Note that the function added to the prototype is using a mixture of ES5 and ES6 function and arrow syntax. The answered Oct 3, 2017 at 19:57
GuyGuy 63.7k94 gold badges250 silver badges318 bronze badges 1 ES6 for..of
answered Dec 2, 2020 at 4:39
caiohamamuracaiohamamura 2,05519 silver badges22 bronze badges A short piece of JavaScript code would do this job:
answered Dec 6, 2014 at 20:23
That way you can put all kinds of exotic things in the array.
I'm only half joking. answered Sep 7, 2016 at 11:06
user40521user40521 1,85919 silver badges7 bronze badges 2 Use
Paul Roub 36k27 gold badges80 silver badges88 bronze badges answered Jun 23, 2017 at 18:40
DamienDamien 1,4951 gold badge13 silver badges24 bronze badges No need to
answered Dec 11, 2017 at 15:34
YasYas 4,2871 gold badge35 silver badges22 bronze badges Here's an elegant one-liner solution that uses stack algorithm, though one may take some time to understand the beauty of this implementation.
Basically, the function accepts an array and checks whether the array contains exactly one item. If false, it pop the last item out of the stack and return the updated array. The beauty of this snippet is that the function includes answered Jan 5, 2018 at 7:48
Rex LowRex Low 1,8862 gold badges18 silver badges45 bronze badges 2 AccuracySort array and start sum form smallest numbers (snippet shows difference with nonsort)
For multidimensional array of numbers use answered Sep 19, 2019 at 7:43
Kamil KiełczewskiKamil Kiełczewski 74.9k26 gold badges335 silver badges308 bronze badges Those are really great answers, but just in case if the numbers are in sequence like in the question ( 1,2,3,4) you can easily do that by applying the formula (n*(n+1))/2 where n is the last number answered Feb 19, 2017 at 10:05
SantoshSantosh 93712 silver badges11 bronze badges 1 You can combine reduce() method with lambda expression:
answered May 14, 2018 at 5:05
antonjsantonjs 13.7k11 gold badges62 silver badges91 bronze badges With reduce()
With forEach()
With Parameter
answered Oct 16, 2021 at 16:55
i saw all answers going for 'reduce' solution
answered Feb 28, 2018 at 14:59
Adrian SwifterAdrian Swifter 791 gold badge1 silver badge9 bronze badges very simple step 1 we should have an array like :
step 2 (you can ignore this step if) step is to be sur that all values in table are number for that
step 3
finally
answered Apr 8, 2021 at 21:56
Simplest answer to understand underlying process:
& if you're already familiar with underlying process then built-in method can save you time:
answered Jul 15, 2021 at 13:21
Not the answer you're looking for? Browse other questions tagged javascript jquery arrays or ask your own question.How do you sum an array?To find the sum of elements of an array.. create an empty variable. ( sum). Initialize it with 0 in a loop.. Traverse through each element (or get each element from the user) add each element to sum.. Print sum.. How do you sum in JavaScript?const num1 = parseInt(prompt('Enter the first number ')); const num2 = parseInt(prompt('Enter the second number ')); Then, the sum of the numbers is computed. const sum = num1 + num2; Finally, the sum is displayed.
Is there a sum method in JavaScript?sum() function in D3. js is used to return the sum of the given array's elements.
How do you sum an array of arrays?Algorithm. Declare and initialize an array.. The variable sum will be used to calculate the sum of the elements. Initialize it to 0.. Loop through the array and add each element of array to variable sum as sum = sum + arr[i].. |