Course Review
Site: | Saylor Academy |
Course: | PRDV401: Introduction to JavaScript I |
Book: | Course Review |
Printed by: | Guest user |
Date: | Monday, October 7, 2024, 10:29 PM |
Description
Congratulations! You've finished the course and have mastered the beginning concepts. We've covered a lot of material, and now it's time for two final practice exercises where you will see how to use JavaScript to solve real-world problems. The first exercise reviews "assignment", "declaration", and "conditional statements" like if/else and switch, and "comparison" and "logical" operators using interactive examples.
Final Practice Exercise Review
You can have it on one condition..!
Human beings (and other animals) make decisions all the time that affect their lives, from small ("should I eat one cookie or two?") to large ("should I stay in my home country and work on my father's farm, or should I move to America and study astrophysics?")
Conditional statements allow us to represent such decision making in JavaScript, from the choice that must be made (for example, "one cookie or two"), to the resulting outcome of those choices (perhaps the outcome of "ate one cookie" might be "still felt hungry", and the outcome of "ate two cookies" might be "felt full, but mom scolded me for eating all the cookies".)
Source: Mozilla, https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
if...else statements
Let's look at by far the most common type of conditional statement you'll use in JavaScript — the humble if...else
statement.
Basic if ... else syntax
Basic if...else
syntax looks like the following in pseudocode:
if (condition) {
code to run if condition is true
} else {
run some other code instead
}
Here we've got:
- The keyword
if
followed by some parentheses. - A condition to test, placed inside the parentheses (typically "is this value bigger than this other value?", or "does this value exist?"). The condition makes use of the comparison operators we discussed in the last module and returns
true
orfalse
. - A set of curly braces, inside which we have some code — this can be any code we like, and it only runs if the condition returns
true
. - The keyword
else
. - Another set of curly braces, inside which we have some more code — this can be any code we like, and it only runs if the condition is not
true
— or in other words, the condition isfalse
.
This code is pretty human-readable — it is saying "if the condition returns true
, run code A, else run code B"
You should note that you don't have to include the else
and the second curly brace block — the following is also perfectly legal code:
if (condition) {
code to run if condition is true
}
run some other code
However, you need to be careful here — in this case, the second block of code is not controlled by the conditional statement, so it always runs, regardless of whether the condition returns true
or false
. This is not necessarily a bad thing, but it might not be what you want — often you want to run one block of code or the other, not both.
As a final point, you may sometimes see if...else
statements written without the curly braces, in the following shorthand style:
if (condition) code to run if condition is true
else run some other code instead
This is perfectly valid code, but using it is not recommended — it is much easier to read the code and work out what is going on if you use the curly braces to delimit the blocks of code, and use multiple lines and indentation.
A Real Example
To understand this syntax better, let's consider a real example. Imagine a child being asked for help with a chore by their mother or father. The parent might say "Hey sweetheart! If you help me by going and doing the shopping, I'll give you some extra allowance so you can afford that toy you wanted." In JavaScript, we could represent this like so:
let shoppingDone = false;
let childsAllowance;
if (shoppingDone === true) {
childsAllowance = 10; } else {
childsAllowance = 5; }
This code as shown always results in the shoppingDone
variable returning false
, meaning disappointment for our poor child. It'd be up to us to provide a mechanism for the parent to set the shoppingDone
variable to true
if the child did the shopping.
Note: You can see a more complete version of this example (also see it running live.)
else if
The last example provided us with two choices, or outcomes — but what if we want more than two?
There is a way to chain on extra choices/outcomes to your if...else
— using else if
. Each extra choice requires an additional block to put in between if() { ... }
and else { ... }
— check out the following more involved example, which could be part of a simple weather forecast application:
<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>
const select = document.querySelector('select');
const para = document.querySelector('p');
select.addEventListener('change', setWeather);
function setWeather() {
const 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 an umbrella, 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 = '';
}
}
- Here we've got an HTML
<select>
element allowing us to make different weather choices, and a simple paragraph. - In the JavaScript, we are storing a reference to both the
<select>
and<p>
elements, and adding an event listener to the<select>
element so that when its value is changed, thesetWeather()
function is run. - When this function is run, we first set a variable called
choice
to the current value selected in the<select>
element. We then use a conditional statement to show different text inside the paragraph depending on what the value ofchoice
is. Notice how all the conditions are tested inelse if() {...}
blocks, except for the first one, which is tested in anif() {...}
block. - The very last choice, inside the
else {...}
block, is basically a "last resort" option — the code inside it will be run if none of the conditions aretrue
. In this case, it serves to empty the text out of the paragraph if nothing is selected, for example, if a user decides to re-select the "--Make a choice--" placeholder option shown at the beginning.
comparison operators
A note on comparison operators
Comparison operators are used to test the conditions inside our conditional statements. We first looked at comparison operators back in our Basic math in JavaScript — numbers and operators article. Our choices are:
===
and!==
— test if one value is identical to, or not identical to, another.<
and>
— test if one value is less than or greater than another.<=
and>=
— test if one value is less than or equal to, or greater than or equal to, another.
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 use a variable name on its own to test whether it is true
, or even that it exists (that is, it is not undefined.) So for example:
let 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:
let shoppingDone = false;
let childsAllowance;
if (shoppingDone) {
childsAllowance = 10;
} else {
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 sunscreen on.';
}
}
Even though the code all works together, each if...else
statement works completely independently of the other one.
logical operators
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 sunscreen 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 (for example, 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.
In such a case, switch
statements are your friend — 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>
const select = document.querySelector('select'); const para = document.querySelector('p'); select.addEventListener('change', setWeather); function setWeather() { const 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 an umbrella, 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 see code from 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 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:
let 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 give her the standard daily greeting.
A Ternary Operator Example
The ternary operator is not just for setting variable values; 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>
const select = document.querySelector('select');
const html = document.querySelector('html');
document.body.style.padding = '10px';
function update(bgColor, textColor) {
html.style.backgroundColor = bgColor;
html.style.color = textColor;
}
select.addEventListener('change', () => select.value ==='black'
? update('black','white')
: update('white','black')
);