# Calculate Standard Deviation in JavaScript

May 1, 2019

Standard deviation is a measure of how far a set of numbers deviates from the average. Small standard deviation means the numbers are all relatively close to the mean. JavaScript doesn't have a built-in standard deviation function, but Math.js is a well supported library with a full featured standard deviation function.

Here's an example of using Math.js' `std()` function to calculate standard deviation.

``````const math = require('mathjs');

// Can pass an array to the `stddev()` function:
math.std([5, 5, 5, 5]); // 0

// Or a list of arguments (also called a "spread")
math.std(1, 5, 9); // 4``````

Math.js also has support for bias correction. Math.js' `std()` function uses Bessel's correction by default, but takes a 2nd argument `normalization` for configuring this. By default, given an array of length `n`, the `std()` function divides the variance by `n - 1`. You can pass `'uncorrected'` to make `std()` divide by `n`, or `'biased'` to make `std()` divide by `n + 1`.

``````const math = require('mathjs');

// Must pass an array if you're using options
math.std([1, 3], 'uncorrected'); // 1
math.std([2, 4, 6, 8], 'biased'); // 2``````

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!