Here’s the one-liner to find the average of an array of numbers in Javascript:
const average = (arr) => arr.reduce((a, b) => a + b, 0) / arr.length;
This uses the reduce()
method to sum up all the elements in the array, and then divides that sum by the length of the array to get the average. The =>
syntax is part of ES6 arrow function notation, which allows for concise function definitions.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.