Skip to content

variance

Calculates the sample variance of an array of numbers.

Syntax

typescript
variance(numbers: number[]): number

Parameters

NameTypeDescription
numbersnumber[]Array of numbers to calculate variance.

Return

TypeDescription
numberThe sample variance of the values.

Example

typescript
variance([2, 4, 4, 4, 5, 5, 7, 9]); // 4
variance([1, 2, 3, 4, 5]); // 2.5

Notes

  • Throws TypeError if the argument is not an array of numbers.
  • Returns NaN for an empty array.
  • Uses the sample variance formula.

References

Released under the MIT License.