Why does this function ever return false?

assuming that an input is not equal to 1 or 2 (e.g. an input of 15), the input would go through the for loop and return false. However, wouldn’t the false return value be overridden by the following ‘return true’ (in the line below)?

help to understand this would be much appreciated.

function checkIfPrime(numb) {
    if (numb === 1) { return false; }
    else if (numb === 2) { return true; } 
    else {
      for (let x = 2; x < numb; x++) {
        if (numb % x === 0) { return false; }
      }
      return true;    
    }
}
console.log(checkIfPrime(2));
console.log(checkIfPrime(15));
console.log(checkIfPrime(17));
console.log(checkIfPrime(19)); 

also if you know a good resource that explains how return values work, that would be much appreciated too.

thank you campers

As soon is you return something the function exits. So the last line is never reached.

1 Like

that was so quick. thank you very much!