var sum = eval(arrayToSum.join('+'));
Basically, this creates a string expression of all of the array values added up (eg,
1+2+3) and then evaluates it with
eval. Note that using
eval is evil if you're not sure what's being passed in -- bad things could happen!
You can also use this to calculate an average, which was how I discovered this method:
var average = eval(arrayToAverage.join('+')) / arrayToAverage.length;
Posted on .