Finding the absolute value : Conditional Operator « Operators « JavaScript Tutorial






<html>
<head>
<title>Finding the absolute value</title>

<script language="javascript" type="text/javascript">
<!--

function f()
{
     var myVar = prompt("A number", "");
     var theModulus = (myVar>=0)? myVar: -myVar;
     var msg = (isNaN(theModulus))? "Numbers only.": "The modulus of " + myVar + " is " + theModulus;
     alert(msg);
}

//-->
</script>

</head>
<body>

<h1>Finding the absolute value</h1>

<a href="javascript:f()">Get absolute value</a>

</body>
</html>








2.8.Conditional Operator
2.8.1.The Conditional Operator and if Statement
2.8.2.Tenary operator
2.8.3.Conditional Operator: ?
2.8.4.Finding the absolute value