代码示例:(标识:js_comparison_17_ternary)
<!DOCTYPE html>
<html>
<body>

<p>输入您的年龄然后点击这个按钮:</p>

<input id="age" value="18" />

<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>

</body>
</html>
运行结果: