JavaScript Program to Find Closest Number in Array

To find closest number in array using JavaScript, we will be understanding and discussing three different approaches. We will also compare time and space complexities of all the approaches used.

In this article we are having an array and a target value, our task is to find closest number to target value in array using JavaScript. User must know about JavaScript arrays, searching algorithms such as linear and binary search, sorting, loops, and conditional statements.

Approaches to Find Closest Number in Array

Here is a list of approaches to find closest number in array in JavaScript which we will be discussing in this article with stepwise explanation and complete example codes.

To find closest number in array using JavaScript, we have used linear search method where we traverse each element in array and compare the closest value.

  • First we declare an array as arr and define a function closestEle() that accepts arr, and target as its arguments.
  • We create a variable closest and set it equal to the first number in the numbers array, and assume that this is the closest number to the target.
  • We then use a for...of loop to iterate through the numbers array. For each element of array we calculate the absolute difference between ele and the target using Math.abs() function and store in currDiff variable.
  • The value of absolute difference of current closest and target is stored in closestDiff variable.
  • Then we use if/else statement to determine closest element. We compare currDiff with closestDiff, if currDiff is less than closestDiff then current element is stored as closest.
  • Finally, the function returns the closest number to the target and is displayed in web console using console.log().

Example

Here is a complete example code implementing above mentioned steps to find closest number in array in JavaScript using linear search.

let arr = [4, 20, 46, 87], target = 63;

function closestEle(arr, target) {
    let closest = arr[0];
    
    for (let ele of arr) {
        let currDiff = Math.abs(ele - target); 
        let closestDiff = Math.abs(closest - target);
        
        if (currDiff 

Closest number to 63 is: 46

In this approach to find closest number in array using JavaScript, we use binary search. Note that this approach requires the array to be sorted.

  • First we declare an array as arr and define a function closestEle() that accepts arr, and target as its arguments. Inside function we define two variables as l and r representing leftmost and rightmost element of the array.
  • Then we calculate the mid element of the array and use Math.floor() function to round down the number.
  • Then, we use if/else statement to compare the target where we first compare the target with mid element of the array. If the target is found we return the array value at the index specified by mid.
  • If the target is not the middle element, then we check if mid element is less than target and set the left pointer as mid + 1; otherwise mid value is assigned to right pointer. This process keeps repeating until left pointer is less than right pointer using while loop.
  • Then we calculate the absolute differences of target and previous value of left pointer, and target and left pointer. These differences are stored in prevDiff and currDiff. These values are then compared using if/else statement.
  • If prevDiff is smaller than currDiff, then value of arr[l-1] is returned.

Example

Here is a complete example code implementing above mentioned steps to find closest number in array in JavaScript using binary search.

let arr = [4, 20, 46, 87], target = 63;

function closestEle(arr, target) {
    let l = 0, r = arr.length - 1;
    
    while (l  0) {
        const prevDiff = Math.abs(arr[l - 1] - target);
        const currDiff = Math.abs(arr[l] - target);
        
        if (prevDiff 

Closest number to 63 is: 46

Using Two Pointer

In this approach, we use two pointer technique to find closest number in array using JavaScript.

  • First we declare an array as arr and define a function closestEle() that accepts arr, and target as its arguments. Inside the function we define two pointers storing leftmost and rightmost index of array and one variable storing first element of array as closest element.
  • Then we use while loop that iterates until left pointer is less than or equal to right pointer. We define three variables inside while loop which store the absolute differences.
  • Then we use two if conditions that compare leftDiff and rightDiff with closestDiff. The conditional statement is again used for the movement of pointers. If the value of array specified by left index is smaller than target, then left pointer is incremented by 1, similarly right pointer is decreased by 1 if value of array specified by right index is greater than target.
  • Then we return the closest variable and result is displayed in web console using console.log().

Example

Here is a complete example code implementing above mentioned steps to find closest number in array in JavaScript using two pointer method.

let arr = [4, 20, 46, 87], target = 63;

function closestEle(arr, target) {
    let left = 0, right = arr.length - 1;
    let closest = arr[0];
    
    while (left  target) {
            right--;
        } else {
            return arr[left];
        }
    }
    return closest;
}

console.log("Closest number to " + target + " is: " + closestEle(arr, target));
Closest number to 63 is: 46

Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Linear Search O(n) O(1)
Binary Search O(log n) O(1)
Two Pointer O(n) O(1)

Conclusion

In this article, we explored three different approaches to find closest number in array using JavaScript: linear search, binary search, and two pointer technique. Binary search provides the best time complexity of O(log n), making it the most efficient approach for sorted arrays.

Updated on: 2026-03-15T23:19:01+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements