【问题标题】:Why doesn't my conditional operator work?为什么我的条件运算符不起作用?
【发布时间】:2016-11-11 00:17:57
【问题描述】:

为什么这个函数返回undefined 而不是“old”?

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

test(15); 

【问题讨论】:

标签: javascript ternary-operator conditional-operator


【解决方案1】:

你的情况很好。你需要return

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

console.log(test(15));

当您离开 return 语句时,函数默认返回 undefined

【讨论】:

    猜你喜欢
    • 2011-06-18
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2015-07-06
    相关资源
    最近更新 更多