It’s rare that I’m disappointed by the JavaScript language not having a function that I need. One such case was summing an array of numbers — I was expecting Math.sum
or a likewise, baked in API. Fear not — summing an array of numbers is easy using Array.prototype.reduce
!
const numbers = [1, 2, 3, 4]; const sum = numbers.reduce((a, b) => a + b, 0);
The 0
represents the starting value while with a
and b
, one represents the running total with the other representing the value to be added. You’ll also note that using reduce
prevents side effects! I’d still prefer something like Math.sum(...numbers)
but a simple reduce
will do!
Create Namespaced Classes with MooTools
MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does. Many developers create their classes as globals which is generally frowned up. I mostly disagree with that stance, but each to their own. In any event…
pointer Media Query
As more devices emerge and differences in device interaction are implemented, the more important good CSS code will become. In order to write good CSS, we need some indicator about device capabilities. We’ve used CSS media queries thus far, with checks for max-width and pixel ratios.