 # standardDeviation javascript Code Snippet in 2022

matharrayintermediate

## Calculates the standard deviation of an array of numbers.

• Use `Array.prototype.reduce()` to calculate the mean, variance and the sum of the variance of the values and determine the standard deviation.
• Omit the second argument, `usePopulation`, to get the sample standard deviation or set it to `true` to get the population standard deviation.
```.css-1gb1dln{color:var(--chakra-colors-white);position:absolute;right:15px;top:12px;cursor:pointer;-webkit-transition:all 0.3s ease-in-out;transition:all 0.3s ease-in-out;}.css-1gb1dln:hover,.css-1gb1dln[data-hover]{-webkit-transform:scale(1.1);-moz-transform:scale(1.1);-ms-transform:scale(1.1);transform:scale(1.1);opacity:0.9;}.css-5ny9vl{width:var(--chakra-sizes-5);height:var(--chakra-sizes-5);display:inline-block;line-height:1em;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;color:#ddd;vertical-align:middle;}.css-1syq16t{padding:var(--chakra-space-2);}```const standardDeviation = (arr, usePopulation = false) => {
const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length;
return Math.sqrt(
arr
.reduce((acc, val) => acc.concat((val - mean) ** 2), [])
.reduce((acc, val) => acc + val, 0) /
(arr.length - (usePopulation ? 0 : 1))
);
};``````
``````standardDeviation([10, 2, 38, 23, 38, 23, 21]); // 13.284434142114991 (sample)
standardDeviation([10, 2, 38, 23, 38, 23, 21], true);
// 12.29899614287479 (population)``````

## Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

Rackpiper Technology Inc