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

<h1>JavaScript 比较</h1>

<h2>() ? : 三元运算符</h2>

<p>请输入您的年龄并单击按钮:</p>

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

<button onclick="myFunction()">试一试</button>

<p id="demo"></p>

<script>
function myFunction() {
  let age = document.getElementById("age").value;
  let voteable = (age < 18) ? "Too young":"Old enough";
  document.getElementById("demo").innerHTML = voteable + " to vote.";
}
</script>

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