在任何的编程语言中,代码需要依靠不同的输入作出决定并且采取行动。例如,在游戏中,如果玩家的生命值变成了0,那么游戏就结束了。在天气应用中,如果在早晨运行,就显示一张日出的图片;如果在晚上,就显示星星和月亮的图片。在这篇文章中,我们将探索在JavaScript中所谓的条件语句是怎样工作的。
预备知识: | 基本的计算机知识,对HTML和CSS有基本的了解,JavaScript的第一步。 |
---|---|
目标: | 了解怎样在JavaScript中使用条件语句的结构。 |
你可以有一个条件……!
人类(以及其他的动物)无时无刻都在做决定,这些决定都影响着他们的生活,从小事(“我应该吃一片还是两片饼干”)到重要的大事(“我应该留在我的祖国在我父亲的农场工作,还是应该去美国学习天体物理学”)
条件语句结构允许我们来描述在JavaScript中这样的选择,从不得不作出的选择(例如:“一片还是两片”)到产生的结果或这些选择(也许是“吃一片饼干”可能会“仍然感觉饿”,或者是“吃两片饼干”可能会“感觉饱了,但妈妈会因为我吃掉了所有的饼干而骂我”。)
if ... else 语句
让我们看看到目前为止你将会在JavaScript中用到的最常见的条件语句类型 — if ... else
statement.
基本的的 if…else 语法
基本的if…else语法看起来像下面的 伪代码:
if (condition) { code to run if condition is true } else { run some other code instead }
在这里我们有:
- 关键字(如果跟着括号的话)。
- 要测试的条件,放到括号里(通常是“这个值大于另一个值码”或者“这个值存在吗”)。这个条件会利用比较运算符(我们会在最后的模块中讨论)比较,并且返回true或者false。
- 一组花括号,在里面我们有一些代码——可以是任何我们喜欢的代码,并且如果条件返回true的话,它就会运行。
- 关键字else。
- 另一组花括号,在里面我们有一些代码——可以是任何我们喜欢的代码,并且如果条件返回的不是true的话,它就会运行。
这段代码真的非常有可读性——它说“如果(if)条件(condition)返回true,运行代码A,否则(else)运行代码B”
你应该注意你不需要一定要包含else和第二个花括号——下面的代码也是完全合法的:
if (condition) { code to run if condition is true } run some other code
不过,你需要注意一下这里——在这种情况下,第二段代码不被条件语句控制,所以它总会运行,不管条件返回的是true还是false。这不一定是一件坏事,但这可能不是你想要的——你经常只想要运行一段代码或者另一段,而不是两个都运行。
最后,有时候你可能会看到 if…else 语句没有写花括号,像下面的速记风格:
if (condition) code to run if condition is true else run some other code instead
这是完全有效的代码,但不建议这样使用——因为(有花括号)会更容易阅读而且如果你用花括号和多行和缩进分割代码块的话,你会明白代码是怎么一回事。。
一个真实的例子
为了更好的理解这种语法,让我们考虑一个真实的例子。想像一个孩子被他的父母要求帮助他们做家务。父母可能会说“嗨,宝贝儿,如果你帮我去购物,我会给你额外的零花钱,这样你就能你想要的玩具了。”在JavaScript中,我们可以这样表示:
var shoppingDone = false; if (shoppingDone === true) { var childsAllowance = 10; } else { var childsAllowance = 5; }
这段代码显示的结果是变量 shoppingDone
总是返回 false
, 意味着对我们的穷孩子来说很失望。如果孩子去购物的话,就需要依靠我们提供机制来使父母把变量 shoppingDone
变成 true
。
Note: 你可以看到在Github上这个例子的完整版本(也可以在线运行)
else if
最后一个例子提供给我们两个选择或结果,但是如果我们想要两个以上呢?
有一种方法来让你的 if…else
连接你的额外的选择和结果——使用else if
。每一个额外的选择要求放到 if() { ... }
和 else { ... }
里——看看下面更多涉及到的例子,它们属于一个普通的天气预报的应用的一部分。
<label for="weather">Select the weather type today: </label> <select id="weather"> <option value="">--Make a choice--</option> <option value="sunny">Sunny</option> <option value="rainy">Rainy</option> <option value="snowing">Snowing</option> <option value="overcast">Overcast</option> </select> <p></p>
var select = document.querySelector('select'); var para = document.querySelector('p'); select.addEventListener('change', setWeather); function setWeather() { var choice = select.value; if (choice === 'sunny') { para.textContent = 'It is nice and sunny outside today. Wear shorts! Go to the beach, or the park, and get an ice cream.'; } else if (choice === 'rainy') { para.textContent = 'Rain is falling outside; take a rain coat and a brolly, and don\'t stay out for too long.'; } else if (choice === 'snowing') { para.textContent = 'The snow is coming down — it is freezing! Best to stay in with a cup of hot chocolate, or go build a snowman.'; } else if (choice === 'overcast') { para.textContent = 'It isn\'t raining, but the sky is grey and gloomy; it could turn any minute, so take a rain coat just in case.'; } else { para.textContent = ''; } }
- 这里我们有 HTML
<select>
元素让我们选择不同的天气,以及一个简单的段落。 - 在 JavaScript 中, 我们同时存储了对
<select>
和<p>
的引用, 并对<select>
添加了一个事件监听器,因此,当它的值改变时,setWeather()
函数被执行。 - 当函数运行时,我们首先新建了一个
choice
变量去存储当前被选的<select>
中的值。接着我们用条件判断语句根据choice
的值选择性的展示段落中的文本。注意else if() {...}
段中的条件是怎么被判断的,除了第一个,它是在if() {...}中被判断的。
- 最后一个
else {...}
中的选择通常被叫做 “最后招数” — 在所有的条件都不为 true 时其中的代码会被执行。在这个例子中,如果用户没有选择任何一个选项,它会将段落中的文本清空,例如当用户决定重新选择最开始出现的"--Make a choice--"选项时,就会有这样的效果。
Note: 你可以 在 GitHub 上找到这个例子 (也可以在线运行。)
关于比较运算符
比较运算符是用来判断条件语句中的条件的。我们先回过头来看看Basic math in JavaScript — numbers and operators 文章中的比较运算符。我们有如下选择:
===
and!==
— 判断一个值是否严格等于,或不等于另一个。<
and>
— 判断一个值是否小于,或大于另一个。<=
and>=
— 判断一个值是否小于或等于,或者大于或等于另一个。
Note: 如果你想复习这些内容,可以回顾之前链接上的材料。
We wanted to make a special mention of testing boolean (true
/false
) values, and a common pattern you'll come across again and again. Any value that is not false
, undefined
, null
, 0
, NaN
, or an empty string (''
) actually returns true
when tested as a conditional statement, therefore you can simply use a variable name on its own to test whether it is true
, or even that it exists (i.e. it is not undefined.) So for example:
var cheese = 'Cheddar'; if (cheese) { console.log('Yay! Cheese available for making cheese on toast.'); } else { console.log('No cheese on toast for you today.'); }
And, returning to our previous example about the child doing a chore for their parent, you could write it like this:
var shoppingDone = false; if (shoppingDone) { // don't need to explicitly specify '=== true' var childsAllowance = 10; } else { var childsAllowance = 5; }
Nesting if ... else
It is perfectly OK to put one if...else
statement inside another one — to nest them. For example, we could update our weather forecast application to show a further set of choices depending on what the temperature is:
if (choice === 'sunny') { if (temperature < 86) { para.textContent = 'It is ' + temperature + ' degrees outside — nice and sunny. Let\'s go out to the beach, or the park, and get an ice cream.'; } else if (temperature >= 86) { para.textContent = 'It is ' + temperature + ' degrees outside — REALLY HOT! If you want to go outside, make sure to put some suncream on.'; } }
Even though the code all works together, each if...else
statement works completely independently of the other one.
Logical operators: AND, OR and NOT
If you want to test multiple conditions without writing nested if...else
statements, logical operators can help you. When used in conditions, the first two do the following:
&&
— AND; allows you to chain together two or more expressions so that all of them have to individually evaluate totrue
for the whole expression to returntrue
.||
— OR; allows you to chain together two or more expressions so that one or more of them have to individually evaluate totrue
for the whole expression to returntrue
.
To give you an AND example, the previous example snippet can be rewritten to this:
if (choice === 'sunny' && temperature < 86) { para.textContent = 'It is ' + temperature + ' degrees outside — nice and sunny. Let\'s go out to the beach, or the park, and get an ice cream.'; } else if (choice === 'sunny' && temperature >= 86) { para.textContent = 'It is ' + temperature + ' degrees outside — REALLY HOT! If you want to go outside, make sure to put some suncream on.'; }
So for example, the first code block will only be run if choice === 'sunny'
and temperature < 86
return true
.
Let's look at a quick OR example:
if (iceCreamVanOutside || houseStatus === 'on fire') { console.log('You should leave the house quickly.'); } else { console.log('Probably should just stay in then.'); }
The last type of logical operator, NOT, expressed by the !
operator, can be used to negate an expression. Let's combine it with OR in the above example:
if (!(iceCreamVanOutside || houseStatus === 'on fire')) { console.log('Probably should just stay in then.'); } else { console.log('You should leave the house quickly.'); }
In this snippet, if the OR statement returns true
, the NOT operator will negate it so that the overall expression returns false
.
You can combine as many logical statements together as you want, in whatever structure. The following example executes the code inside only if both OR statements return true, meaning that the overall AND statement will return true:
if ((x === 5 || y > 3 || z <= 10) && (loggedIn || userName === 'Steve')) { // run the code }
A common mistake when using the logical OR operator in conditional statements is to try to state the variable whose value you are checking once, and then give a list of values it could be to return true, separated by ||
(OR) operators. For example:
if (x === 5 || 7 || 10 || 20) { // run my code }
In this case the condition inside if(...)
will always evaluate to true since 7 (or any other non-zero value) always evaluates to true. This condition is actually saying "if x equals 5, or 7 is true — which it always is". This is logically not what we want! To make this work you've got to specify a complete test either side of each OR operator:
if (x === 5 || x === 7 || x === 10 ||x === 20) { // run my code }
switch statements
if...else
statements do the job of enabling conditional code well, but they are not without their downsides. They are mainly good for cases where you've got a couple of choices, and each one requires a reasonable amount of code to be run, and/or the conditions are complex (e.g. multiple logical operators). For cases where you just want to set a variable to a certain choice of value or print out a particular statement depending on a condition, the syntax can be a bit cumbersome, especially if you've got a large number of choices.
switch
statements are your friend here — they take a single expression/value as an input, and then look through a number of choices until they find one that matches that value, executing the corresponding code that goes along with it. Here's some more pseudocode, to give you an idea:
switch (expression) { case choice1: run this code break; case choice2: run this code instead break; // include as many cases as you like default: actually, just run this code }
Here we've got:
- The keyword
switch
, followed by a set of parentheses. - An expression or value inside the parentheses.
- The keyword
case
, followed by a choice that the expression/value could be, followed by a colon. - Some code to run if the choice matches the expression.
- A
break
statement, followed by a semi-colon. If the previous choice matches the expression/value, the browser stops executing the code block here, and moves on to any code that appears below the switch statement. - As many other cases (bullets 3–5) as you like.
- The keyword
default
, followed by exactly the same code pattern as one of the cases (bullets 3–5), except thatdefault
does not have a choice after it, and you don't need tobreak
statement as there is nothing to run after this in the block anyway. This is the default option that runs if none of the choices match.
Note: You don't have to include the default
section — you can safely omit it if there is no chance that the expression could end up equaling an unknown value. If there is a chance of this however, you need to include it to handle unknown cases.
A switch example
Let's have a look at a real example — we'll rewrite our weather forecast application to use a switch statement instead:
<label for="weather">Select the weather type today: </label> <select id="weather"> <option value="">--Make a choice--</option> <option value="sunny">Sunny</option> <option value="rainy">Rainy</option> <option value="snowing">Snowing</option> <option value="overcast">Overcast</option> </select> <p></p>
var select = document.querySelector('select'); var para = document.querySelector('p'); select.addEventListener('change', setWeather); function setWeather() { var choice = select.value; switch (choice) { case 'sunny': para.textContent = 'It is nice and sunny outside today. Wear shorts! Go to the beach, or the park, and get an ice cream.'; break; case 'rainy': para.textContent = 'Rain is falling outside; take a rain coat and a brolly, and don\'t stay out for too long.'; break; case 'snowing': para.textContent = 'The snow is coming down — it is freezing! Best to stay in with a cup of hot chocolate, or go build a snowman.'; break; case 'overcast': para.textContent = 'It isn\'t raining, but the sky is grey and gloomy; it could turn any minute, so take a rain coat just in case.'; break; default: para.textContent = ''; } }
Note: You can also find this example on GitHub (see it running live on there also.)
Ternary operator
There is one final bit of syntax we want to introduce you to, before we get you to play with some examples. The ternary or conditional operator is a small bit of syntax that tests a condition and returns one value/expression if it is true
, and another if it is false
— this can be useful in some situations, and can take up a lot less code than an if...else
block if you simply have two choices that are chosen between via a true
/false
condition. The pseudocode looks like this:
( condition ) ? run this code : run this code instead
So let's look at a simple example:
var greeting = ( isBirthday ) ? 'Happy birthday Mrs. Smith — we hope you have a great day!' : 'Good morning Mrs. Smith.';
Here we have a variable called isBirthday
— if this is true
, we give our guest a happy birthday message; if not, we given her the standard daily greeting.
Ternary operator example
You don't just have to set variable values with the ternary operator; you can also run functions, or lines of code — anything you like. The following live example shows a simple theme chooser where the styling for the site is applied using a ternary operator.
<label for="theme">Select theme: </label> <select id="theme"> <option value="white">White</option> <option value="black">Black</option> </select> <h1>This is my website</h1>
var select = document.querySelector('select'); var html = document.querySelector('html'); document.body.style.padding = '10px'; function update(bgColor, textColor) { html.style.backgroundColor = bgColor; html.style.color = textColor; } select.onchange = function() { ( select.value === 'black' ) ? update('black','white') : update('white','black'); }
Here we've got a <select>
element to choose a theme (black or white), plus a simple <h1>
to display a website title. We also have a function called update()
, which takes two colors as parameters (inputs). The website's background color is set to the first provided color, and its text color is set to the second provided color.
Finally, we've also got an onchange event listener that serves to run a function containing a ternary operator. It starts with a test condition — select.value === 'black'
. If this returns true
, we run the update()
function with parameters of black and white, meaning that we end up with background color of black and text color of white. If it returns false
, we run the update()
function with parameters of white and black, meaning that the site color are inverted.
Note: You can also find this example on GitHub (see it running live on there also.)
Active learning: A simple calendar
In this example you are going to help us finish a simple calendar application. In the code you've got:
- A
<select>
element to allow the user to choose between different months. - An
onchange
event handler to detect when the value selected in the<select>
menu is changed. - A function called
createCalendar()
that draws the calendar and displays the correct month in the<h1>
element.
We need you to write a conditional statement inside the onchange
handler function, just below the // ADD CONDITIONAL HERE
comment. It should:
- Look at the selected month (stored in the
choice
variable. This will be the<select>
element value after the value changes, so "January" for example.) - Set a variable called
days
to be equal to the number of days in the selected month. To do this you'll have to look up the number of days in each month of the year. You can ignore leap years for the purposes of this example.
Hints:
- You are advised to use logical OR to group multiple months together into a single condition; many of them share the same number of days.
- Think about which number of days is the most common, and use that as a default value.
If you make a mistake, you can always reset the example with the "Reset" button. If you get really stuck, press "Show solution" to see a solution.
Playable code
<div class="output" style="height: 500px;overflow: auto;"> <label for="month">Select month: </label> <select id="month"> <option value="January">January</option> <option value="February">February</option> <option value="March">March</option> <option value="April">April</option> <option value="May">May</option> <option value="June">June</option> <option value="July">July</option> <option value="August">August</option> <option value="September">September</option> <option value="October">October</option> <option value="November">November</option> <option value="December">December</option> </select> <h1></h1> <ul></ul> </div> <hr> <textarea id="code" class="playable-code" style="height: 500px;"> var select = document.querySelector('select'); var list = document.querySelector('ul'); var h1 = document.querySelector('h1'); select.onchange = function() { var choice = select.value; // ADD CONDITIONAL HERE createCalendar(days, choice); } function createCalendar(days, choice) { list.innerHTML = ''; h1.textContent = choice; for (var i = 1; i <= days; i++) { var listItem = document.createElement('li'); listItem.textContent = i; list.appendChild(listItem); } } createCalendar(31,'January'); </textarea> <div class="playable-buttons"> <input id="reset" type="button" value="Reset"> <input id="solution" type="button" value="Show solution"> </div>
.output * { box-sizing: border-box; } .output ul { padding-left: 0; } .output li { display: block; float: left; width: 25%; border: 2px solid white; padding: 5px; height: 40px; background-color: #4A2DB6; color: white; }
var textarea = document.getElementById('code'); var reset = document.getElementById('reset'); var solution = document.getElementById('solution'); var code = textarea.value; function updateCode() { eval(textarea.value); } reset.addEventListener('click', function() { textarea.value = code; updateCode(); }); solution.addEventListener('click', function() { textarea.value = jsSolution; updateCode(); }); var jsSolution = 'var select = document.querySelector(\'select\');\nvar list = document.querySelector(\'ul\');\nvar h1 = document.querySelector(\'h1\');\n\nselect.onchange = function() {\n var choice = select.value;\n var days = 31;\n if(choice === \'February\') {\n days = 28;\n } else if(choice === \'April\' || choice === \'June\' || choice === \'September\'|| choice === \'November\') {\n days = 30;\n }\n\n createCalendar(days, choice);\n}\n\nfunction createCalendar(days, choice) {\n list.innerHTML = \'\';\n h1.textContent = choice;\n for(var i = 1; i <= days; i++) {\n var listItem = document.createElement(\'li\');\n listItem.textContent = i;\n list.appendChild(listItem);\n }\n }\n\ncreateCalendar(31,\'January\');'; textarea.addEventListener('input', updateCode); window.addEventListener('load', updateCode);
Active learning: More color choices!
In this example you are going to take the ternary operator example we saw earlier and convert the ternary operator into a switch statement that will allow us to apply more choices to the simple website. Look at the <select>
— this time you'll see that it has not two theme options, but five. You need to add a switch statement just underneath the // ADD SWITCH STATEMENT
comment:
- It should accept the
choice
variable as its input expression. - For each case, the choice should equal one of the possible values that can be selected, i.e. white, black, purple, yellow, or psychedelic.
- For each case, the
update()
function should be run, and be passed two color values, the first one for the background color, and the second one for the text color. Remember that color values are strings, so need to be wrapped in quotes.
If you make a mistake, you can always reset the example with the "Reset" button. If you get really stuck, press "Show solution" to see a solution.
Playable code 2
<div class="output" style="height: 300px;"> <label for="theme">Select theme: </label> <select id="theme"> <option value="white">White</option> <option value="black">Black</option> <option value="purple">Purple</option> <option value="yellow">Yellow</option> <option value="psychedelic">Psychedelic</option> </select> <h1>This is my website</h1> </div> <hr> <textarea id="code" class="playable-code" style="height: 450px;"> var select = document.querySelector('select'); var html = document.querySelector('.output'); select.onchange = function() { var choice = select.value; // ADD SWITCH STATEMENT } function update(bgColor, textColor) { html.style.backgroundColor = bgColor; html.style.color = textColor; }</textarea> <div class="playable-buttons"> <input id="reset" type="button" value="Reset"> <input id="solution" type="button" value="Show solution"> </div>
var textarea = document.getElementById('code'); var reset = document.getElementById('reset'); var solution = document.getElementById('solution'); var code = textarea.value; function updateCode() { eval(textarea.value); } reset.addEventListener('click', function() { textarea.value = code; updateCode(); }); solution.addEventListener('click', function() { textarea.value = jsSolution; updateCode(); }); var jsSolution = 'var select = document.querySelector(\'select\');\nvar html = document.querySelector(\'.output\');\n\nselect.onchange = function() {\n var choice = select.value;\n\n switch(choice) {\n case \'black\':\n update(\'black\',\'white\');\n break;\n case \'white\':\n update(\'white\',\'black\');\n break;\n case \'purple\':\n update(\'purple\',\'white\');\n break;\n case \'yellow\':\n update(\'yellow\',\'darkgray\');\n break;\n case \'psychedelic\':\n update(\'lime\',\'purple\');\n break;\n }\n}\n\nfunction update(bgColor, textColor) {\n html.style.backgroundColor = bgColor;\n html.style.color = textColor;\n}'; textarea.addEventListener('input', updateCode); window.addEventListener('load', updateCode);
Conclusion
And that's all you really need to know about conditional structures in JavaScript right now! I'm sure you'll have understood these concepts and worked through the examples with ease; if there is anything you didn't understand, feel free to read through the article again, or contact us to ask for help.