eSlack eSlack - 21 days ago 5
Javascript Question

Why doesn't my conditional operator work?

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

test(15);


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

Answer

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.

Comments