|
| JAVASCRIPT CONDITIONAL OPERATOR
The conditional operator, also known as the ternary operator, is used to evaluate a specified condtion and if true executes a certain statement and if false executes another statement.
The general format is a follows:
(specified condition)? if true do this:if false do this;
Example:
Say we have two members, John and Nancy. We want to only award a $1000 prize to Nancy, otherwise the prize is $0.
We declare two variables:
var member;
var prize;
Only if var member is Nancy, must var prize be $1000, otherwise it is $0. We can write it as follows:
prize=(member=='Nancy')?'$1000':'$0';
|
|