I am getting the boolean values that I want in console but I am also getting undefined after each boolean output. Why is this the case?

function hasPassingGrade(score) {
  switch (true) {
    case score > 59 :
        console.log("true");
        break
    case  score < 59 :
        console.log("false")

  }
  
}


console.log(hasPassingGrade(100));
console.log(hasPassingGrade(53));
console.log(hasPassingGrade(87));
``

undefined is the default value returned by functions in JavaScript. The true/false that’s output to the console, is from the within function - the console.log("true") or console.log("false"). Then is printed undefined since function doesn’t return anything.

Please post your questions in the body of your post instead of just the title.

You’re seeing undefined because your function hasPassingGrade does not return anything; it only logs values. JavaScript functions implicitly return undefined if no return statement is used.

Fix it by returning the boolean values instead of logging them:

removed

Now, console.log prints only the boolean values.

#happycoding JavaScript Code Feedback

Welcome to the forum :wave:

Please do not share solution code.

1 Like