JavaScript solution with explanation (100% pass) 
I did not find an elegant JavaScript solution. Thanks to Leetcode, I got inspired by one of the posts, I came up with this solution for folks with JS background
I did not find an elegant JavaScript solution. Thanks to Leetcode, I got inspired by one of the posts, I came up with this solution for folks with JS background.\n\n```\nvar findClosestElements = function(arr, k, x) {\n    //-------- Main idea behind the binary search algorithm ----------//\n    // 1) res will be a consecutive subarray of k size\n    // 2) say if we need to pick 4 elems, now we r looking at 5 elem n1, n2, n3, n4, n5\n    // we need to compare two ends: diff(x, n1) and diff(x, n5), \n    // the number with bigger diff on the end will be eleminated\n    //----------------------------------------------------------------//\n    // lo and hi: range of all possible start of subarray with size k + 1, so we could compare both ends\n    let low = 0, hi = arr.length -k;\n    while(low <hi) {\n        let mid = Math.floor((low+hi)/2);\n        // for subarray starting at mid with size k+1, we compare element of two ends to eliminate the loser\n        if(x-arr[mid] > arr[mid+k]-x) {\n            low = mid+1; // arr[mid] is the one further away from x, eliminate range[lo, mid]\n        } else {\n            hi = mid; // arr[mid+k] is the one further away, all [mid, hi] will have simiar situation, elimiate them\n        }\n    }\n    return arr.slice(low, low+k);\n};\n```\n\nTime Complexity: The binary search costs O(log n) (actually also just O(log (n-k))
[]