If
最简单的条件判断是if语句,语法是 if(condition){ do this … }
。条件判断为真,才执行分支中的代码。举个字符串的例子:
var country = 'France';
var weather;
var food;
var currency;
if(country === 'England') {
weather = 'horrible';
food = 'filling';
currency = 'pound sterling';
}
if(country === 'France') {
weather = 'nice';
food = 'stunning, but hardly ever vegetarian';
currency = 'funny, small and colourful';
}
if(country === 'Germany') {
weather = 'average';
food = 'wurst thing ever';
currency = 'funny, small and colourful';
}
var message = 'this is ' + country + ', the weather is ' +
weather + ', the food is ' + food + ' and the ' +
'currency is ' + currency;
注意: 条件判断可以嵌套。
Exercise
Correct!
var name =
if (name === "John") {
}
var name = "John"; if (name === "John") { }
assert(name === "John");