Revision: 16307
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 30, 2009 14:51 by funkadelicsoul
Initial Code
var myVar = 1,
newVar;
/**
* Standard if else statement
*/
if (myVar === 1) {
newVar = 'One';
} else {
newVar = 'Anything else';
}
alert(newVar); //One
/**
* Same as this standard ternary expression
*/
newVar = (myVar === 1) ? 'One' : 'Anything else';
alert(newVar); //One
myVar = 2;
/**
* Embedded if statement
*/
if (myVar) {
if (myVar === 1) {
newVar = 'One';
} else {
newVar = 'Two';
}
} else {
newVar = 'Anything else';
}
alert(newVar); //Two
/**
* Same as this ternary expression
*/
newVar = (myVar) ? ((myVar == 1) ? 'One' : 'Two' ) : 'Anything else';
alert(newVar); //Two
myVar = 'Foo Bar';
/**
* If Then Else If statement
*/
if (myVar === 1) {
newVar = 'One';
} else if (myVar === 2) {
newVar = 'Two';
} else {
newVar = 'Anything else';
}
alert(newVar); //Anything else
/**
* Same as this ternary expression
*/
newVar = (myVar == 1) ? 'One' : (myVar == 2) ? 'Two' : 'Anything else' ;
alert(newVar); //Anything else
Initial URL
Initial Description
Initial Title
Ternary expressions
Initial Tags
Initial Language
JavaScript