eSlack eSlack - 1 year ago 75
Javascript Question

Why doesn't my conditional operator work?

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


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

Answer Source

Your condition is fine. You need to return

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


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download