鍍金池/ 教程/ HTML/ If
注釋
對象
創(chuàng)建
基本運算符
數(shù)據(jù)類型
函數(shù)
For 循環(huán)
Do...While 循環(huán)
長度
While 循環(huán)
比較運算符
索引
變量
高級運算符
枚舉
條件邏輯
If
原型
可變性
數(shù)字
創(chuàng)建
編程基礎(chǔ)
屬性
銷毀
創(chuàng)建
等式
長度
函數(shù)聲明
字符串
條件連接
循環(huán)
連接
引用
Else
數(shù)組
高階函數(shù)
全局化

If

最簡單的條件判斷是if語句,語法是 if(condition){ do this … } 。條件判斷為真,才執(zhí)行分支中的代碼。舉個字符串的例子:

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 %} 填寫 name 的值,驗證條件判斷。 {% initial %} var name =

if (name === "John") {

} {% solution %} var name = "John";

if (name === "John") {

} {% validation %} assert(name === "John"); {% endexercise %}

上一篇:基本運算符下一篇:比較運算符