Here is a revised, simpler version of your code. function indexFirstLast(target, arr) { let [first, last] = [-1, -1]; for (let i = 0; i < arr.length && arr[i] <= target; i++) { if (arr[i] === target) { if (first === -1) { first = i; } last = i; } } return [first, last]; }