Ternary Operators In Java Script

Ternary operator require one expression, on variable or value for true case and one variable or value for false case. If the condition in given expression is found true it assigns value to variable present in true value position else the value of false value position will be assigned.
 
variable = condition ? true value : false value
 
Variable to which you want to assign value = condition ? in case of condition is true this value will be assigned : in case of condition is false this value will be assigned
like:
var p=90
var a = p>100 ? 25:50
 
p is having value 90 we are comparing that whether p is greater than 100 which is false because p is 90so the value 50 will be assigned to a. If p =150 then value 25 will be assigned to a.
 
 
Example
 
<html>

<head>

</head>

<body>

<script type=”text/javascript”>

var a=50

var b b= a>=50 ? “Greater then or equal to 50 ” : “Lesser than 50” document.write(“<br>Value of a “+a);

document.write(“<br>Value of b “+b);

</script >

</body>

</html>

 
Understanding program :
value of a is 50 we are checking that if a is greater than or equal to 50 then “Greater than or equal to 50” should be assigned to it else “Lesser than 50” should be assigned to variable b . here value of a is 50 so the condition becomes true and “Greater than or equal to 50” will be placed in variable b.
 
Output:
 
Value of a 50
Value of b Greater then or equal to 50