voteable = (age < 18) ? "好年轻":"够成熟";
如果变量 age 的值小于 18,变量 voteable 的值将是 "太年轻",否则变量 voteable 的值将是 "足够成熟"。
操作例子
<button onclick="myFunction()">试一试</button>
<p id="demo"></p>
<script>
function myFunction() {
var age, voteable;
age = document.getElementById("age").value;
voteable = (age < 18) ? "好年轻":"够成熟";
document.getElementById("demo").innerHTML = voteable;
}
</script>
https://www.w3school.com.cn/tiy/t.asp?f=js_comparison_17_ternary