How to find the median value of an array using JavaScript

In this post, we will learn about the median value and how to find the median of an array of numbers using JavaScript.

So what is a median value?

In simple words, a median is the middle value of a sorted, ascending, or descending list of numbers.

Example, the median of [5,3,2,1,4] is 3.

However, finding the median value in an array will have two approaches depending on the array length.

  1. For odd length of an array
  2. For even length of an array

How to find the median whose array length is odd?

For the odd lengths of the array, finding the median number is very easy.

First, sort the items of the array in ascending and descending order and then find the middle value.

For example, let’s say we have an array [3, 1, 2, 5, 7, 6, 4] .

First, sort the number in ascending order : [1, 2, 3, 4, 5, 6, 7]

So, the median value will be [1, 2, 3, 4, 5, 6, 7] = 4

Let’s find the median with odd array.length using JavaScript.

function findMedian(arr){

  // getting the length of the array
  const length = arr.length;

  // sorting array in ascending order
  const sortedArr = arr.sort((a,b)=> {return a-b})

  // getting the mid index of the array
  const middle = Math.floor(length/2)

  // return the median value present in the middleIndex
  return sortedArr[middle];
} 

console.log('Median:',findMedian(arr)) // 4

Output:

Median : 4

How to find the median value if the array length is even?

If the length of the array is even, first, sort the array then we have to find the middle two numbers and find the average of the two numbers.

The example, suppose this is the array [3, 1, 2, 8, 5, 7, 6, 4].

First, sort it in ascending order : [1, 2, 3, 4, 5, 6, 7, 8].

The middle two values are 4 and 5.

So the median will be (4+5)/2 = 4.5

Now, let’s solve it using a JavaScript program to find the median of an array with an even array length.

const arr = [3,1,2,5,7,6,4,8]

function findMedian(arr){

  // getting the length of the array
  const length = arr.length;

  // sorting array in ascending order
  const sortedArr = arr.sort((a,b)=> {return a-b})

  // getting the first middle value index
  const firstMid = Math.floor(length/2);

  // getting the second middle value index
  const secondMid = Math.floor(length/2) - 1;

  // return the median value
  return (sortedArr[firstMid] + sortedArr[secondMid])/2;
} 

console.log(findMedian(arr)) // 4.5

Now we can combine them both with an if...else statement, so that it will return the median of the array depending on its length of it.

const arr = [9,4,6,3,8]

function findMedian(arr){

  const length = arr.length;

  //check if the array length is odd or even
  if(length%2 !=0 ){

    const sortedArr = arr.sort((a,b)=> {return a-b})

    // getting the mid index of the array
    const middle = Math.floor(length/2)

    // return the median value present in the middleIndex
    return sortedArr[middle];

  } else {

    // sorting array in ascending order
    const sortedArr = arr.sort((a,b)=> {return a-b})

    // getting the first middle value index
    const firstMid = Math.floor(length/2);

    // getting the second middle value index
    const secondMid = Math.floor(length/2) - 1;

    // return the median value present in the middleIndex
    return (sortedArr[firstMid] + sortedArr[secondMid])/2;
  }
} 

console.log(findMedian(arr))

Code Explanation:

arr.length : to get the length of the given array.

arr.sort() : It is used to sort the items in an array.

  • If you return a - b it will be in ascending order,
  • If you return b - a it will be in descending order.

Math.floor() : It returns an integer value less than or equal to a given number.

And Math.floor(length/2) gives us the index of the middle number in the sorted array.

sortedArr[middle] gives us the value present in that particular index of the sorted array.

Related Article:

Find the average of n numbers using JavaScript

Scroll to Top