var sum = array.reduce((a, b) => a + b, 0); //get sum of all elements in array var avg = (sum / array.length) || 0; //get average of all elements in array 😉
Here is what the above code is Doing:
1. We’re using the reduce() method to get the sum of all the elements in the array.
2. We’re dividing the sum by the length of the array to get the average.
3. We’re using the || operator to set the default value of avg to 0 if the array is empty.