Ternary operator( ?: )
Returns one of two expressions depending on
a condition.
Syntax
test ? expression1 : expression2
Returns
expression1 if the “test” condition evaluates to true other wise returns
expression2.
Example
var x =
prompt("Enter an Integer", "100");
var y = (x
> 10)? "x is greater than 10": "x is less than 11";
If you
enter a value which is > 10, then y is assigned with message "x is
greater than 10", else y is assigned with message "x is less than
11".
ternary.html
<!DOCTYPE html> <html> <head> <title>Ternary</title> </head> <body> <script type="text/javascript"> var x = prompt("Enter an Integer", "100"); var y = (x > 10) ? "x is greater than 10" : "x is less than 11"; document.write("value of x is " + x + "<br />"); document.write(y); </script> </body> </html>
No comments:
Post a Comment