-1

Why does this function return undefined instead of "old"?

function test(age) {
  12 < age ? "old" : "young";
}

test(15); 
jacefarm
  • 6,747
  • 6
  • 36
  • 46
eSlack
  • 13
  • 1

1 Answers1

3

Your condition is fine. You need to return

function test(age) {
  return 12 < age ? "old" : "young";
}

console.log(test(15));

When you leave off a return statement, a function returns undefined by default.

KevBot
  • 17,900
  • 5
  • 50
  • 68