Observation in "JavaScript Algorithms and Data Structures Projects: Telephone Number Validator" exersice

I am trying to RegExp replace all the non-alphanumeric characters and the console.log isn’t showing the result BUT if I change the position of the console.log, the result will be correct. This is when it is working:

function telephoneCheck(str) {
  let rplStr = str.replace(/\W/g, "")
  let splStr = rplStr.split('')
  
  console.log(rplStr)
  if (splStr.length - 1 === 10 && splStr[0].charCodeAt() === 49) {
    return true
  } else if (splStr.length - 1 === 9) {
    return true
  }
 
}

telephoneCheck("1 (555) 555-5555");

AND this is when the console.log is not showing anything:

function telephoneCheck(str) {
  let rplStr = str.replace(/\W/g, "")
  let splStr = rplStr.split('')
  
  if (splStr.length - 1 === 10 && splStr[0].charCodeAt() === 49) {
    return true
  } else if (splStr.length - 1 === 9) {
    return true
  }
 console.log(rplStr)
}

telephoneCheck("1 (555) 555-5555");

Why console.log isn’t showing the result in the second example?

The console log isn’t showing in the second case because it’s after a return statement. No code is executed after the function returns.

2 Likes