Proposal: Math.add, Math.sub
Ates Goral
ates at magnetiq.com
Sat May 11 23:20:57 UTC 2019
(Percolating a comment I made in a thread earlier.)
# Math.add
```
Math.add = function (a, b) {
return a + b;
};
```
A why:
With functional-style programming becoming more and more common with
the advent of ES5 Array methods, the terse way to find the sum of
items in an array can become even more terse with the introduction of
`Math.add`:
```
const sum = array.reduce((a, b) => a + b);
// becomes:
const sum = array.reduce(Math.add);
```
# Math.sub
```
Math.sub = function (a, b) {
return a - b;
};
```
A [not-so-strong] why:
It's a common "LOL WTF JavaScript" refrain to point out that
attempting to sort an array of numbers by using the default
`Array.prototype.sort` results in an unexpected sort order unless a
custom comparator is used. The presence of an out-of-the-box
`Math.sub` could make the canonical "proper way to sort numbers" a bit
terser:
```
array.sort((a, b) => a - b);
// becomes:
array.sort(Math.sub);
```
Ates
More information about the es-discuss
mailing list