B
Your option reducebut ecmascript6♪
Either way, we'll have to move the whole body, and as best as we do, it's up to you to decide.const abs = Math.abs;
const closest = (a,g) => a.reduce((p,c) => abs(c-g) < abs(p-g) ? c : p);
closest([3, 40, 90, 197], 4); // 3
closest([1, 2, 9], -5); // 1
closest([1, 2, 9], 15); // 9
// document.getElementById("demo").innerHTML = closest([3, 40, 90, 197], 4);