Use a conditional (ternary) operator to compare age - Javascript Operator

Javascript examples for Operator:Quiz

Description

Use a conditional (ternary) operator to compare age

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

Age: <input type="number" id="age" value="18">
<button onclick="checkAge()">Check Age</button>
<p id="demo"></p>

<script>
function checkAge() {/*ww w.ja  v  a 2s . c  o m*/
  var age = document.getElementById("age").value;
  var voteable = (age < 18) ? "Too young":"Old enough";
  document.getElementById("demo").innerHTML = voteable + " to vote.";
}
</script>

</body>
</html>

Related Tutorials