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

Javascript examples for Operator:Quiz

Description

Use a conditional (ternary) operator to compare string values

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

First Name: <input type="text" id="fname" value="TOM">
<button onclick="checkName()">Check Name</button>
<p id="demo"></p>

<script>
function checkName() {//from w w  w.  java2 s  . c om
  var firstName = document.getElementById("fname").value;
  var result = (firstName === "TOM") ? "Hello TOM!":"You're not TOM!";
  document.getElementById("demo").innerHTML = result;
}
</script>

</body>
</html>

Related Tutorials