Different outputs

:wave:
What is the difference among these codes?
Thank you

const myNumAsCharArr = ['1','2','4'];

const missedNum = '3';


function checkNumExist(arrOfChars, numStr){
  let completeArr = myNumAsCharArr.slice();  
  if (myNumAsCharArr.indexOf(missedNum) === -1) completeArr.push(missedNum)
  else completeArr
  return completeArr
}

checkNumExist(myNumAsCharArr, missedNum); //--> [ '1', '2', '4', '3' ]


const checkNumExist = (arrOfChars, numStr) => {
  let completeArr = myNumAsCharArr.slice();
  
  return myNumAsCharArr.indexOf(missedNum) === -1 
  ? completeArr.push(missedNum)
  : completeArr
}

 checkNumExist(myNumAsCharArr, missedNum) //--> 4



function checkNumExist(arrOfChars, numStr){
  let completeArr = myNumAsCharArr.slice();  
  if (myNumAsCharArr.indexOf(missedNum) === -1) return completeArr.push(missedNum)
  return completeArr   
}

checkNumExist(myNumAsCharArr, missedNum); // --> 4

Why don’t you see if you can tell us the differences? We can correct you if you are missing anything.

The above would be the equivalent of:

function checkNumExist (arrOfChars, numStr) {
  let completeArr = myNumAsCharArr.slice();
  if (myNumAsCharArr.indexOf(missedNum) === -1) {
    return completeArr.push(missedNum);
  } else {
    return completeArr;
  }
}

the first code is different from the others cause the conditional statement is storing a value in an external var but it’s not returning the final value of that var. In the other programs I try to make an operation derived of the conditional AND at the same time return the final value stored.

I’m missing why I can’t do that?
I don’t get why the last 2 programs are returning just the missed element 4
am I deleting the rest of the numbers in completeArr?

(I couldn’t observe the hold process in a debugger)