Else
当第一个条件语句不成立时, else
语句将被执行。如果你想要在特殊条件下才返回一个值,这非常有效:
var umbrellaMandatory;
if(country === 'England'){
umbrellaMandatory = true;
} else {
umbrellaMandatory = false;
}
else
语句可以和另一个 if
语句结合。改造一下上面的例子:
if(country === 'England') {
...
} else if(country === 'France') {
...
} else if(country === 'Germany') {
...
}
Exercise
Correct!
var name =
if (name === "John") {
} else if (name === "Aaron") {
// Valid this condition
}
var name = "Aaron"; if (name === "John") { } else if (name === "Aaron") { // Valid this condition }
assert(name === "Aaron");