Calculate the median value of an array of numbers.
Code
Generalconst sorted = [...arr].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;Parameters
Array of numbers
Browser·fetch() may be limited by CORS
More JavaScript Snippets
Binary to Decimal
Convert a binary string representation to its decimal number equivalent.
Bytes to Human Readable
Convert bytes to human readable format (KB, MB, GB, etc.) for file sizes.
Calculate Mode
Find the most frequent value(s) in an array.
Calculate Percentile
Calculate the nth percentile of an array using linear interpolation.
Celsius to Fahrenheit
Convert temperature from Celsius to Fahrenheit using the standard formula.
Clamp Number
Restrict a number to be within a minimum and maximum range.