Understanding undefined return with [] array

Tell us what’s happening: Hi! I’m trying to complete one of the challenges (Basic Algorithm Scripting: Where do I belong), and I have all conditions covered except where arr is an empty array []. As my code shows, I try to check for this first with an if statement, but instead the function returns “undefined” instead of 0. Every other conditions seems to work fine. I would appreciate any guidance for what I might be missing! I can think of some other ways to solve this challenge, but now I just really want to know what is wrong with what I have so far with the empty array check at the top. Thanks!

Your code so far

function getIndexToIns(arr, num) {
  if (arr == []) {
    return 0;
  } else {
    arr.sort((a, b) => a - b);
    for (let i = 0; i < arr.length; i++) {
      if (num > arr[arr.length-1]) {
        return arr.length;
      if (num <= arr[i]) {
        return i;

getIndexToIns([], 1);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36.

instead of “arr ==[]” or “arr===[]” i would put “arr.length === 0”.
This is beacause i think you do a reference comparision (related with positions in memory)

When you compare arr to [], you are actually checking whether or not the arrays have the same reference in memory. You are not actually comparing the values in the array, so in this case, they would never be equal. The reason your function returns undefined is because since you do not have an explicit return statement (none of the if statements evaluate to true), the value undefined gets returned by default.