The answer has been edited to avoid the problem of "overwriting the function with the result" identified by @pimvdb and kindly explained by @some (in the comments below).
A simple approach that should cover almost all browsers (I think) is to use a function to sum the values ββof the array:
var vals = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; function arraySum(arr) { if (!arr) { return false; } else { var sum = 0; for (var i = 0, len = arr.length; i < len; i++) { sum += arr[i]; } return sum; } } sum = arraySum(vals.sort(function(a, b) { return b - a; }).slice(0, 6)); console.log(sum);β
JS Fiddle demo .
Although for those browsers in which it is available, reduce() much simpler.
source share