Open this example in the interactive library
Source code
const xs = [9, 1, 4, 6].sort((a, b) => a - b),
mid = Math.floor(xs.length / 2);
console.log(xs.length % 2 ? xs[mid] : (xs[mid - 1] + xs[mid]) / 2);
Sample output
5

Open this example in the interactive library
const xs = [9, 1, 4, 6].sort((a, b) => a - b),
mid = Math.floor(xs.length / 2);
console.log(xs.length % 2 ? xs[mid] : (xs[mid - 1] + xs[mid]) / 2);
5