Course Review
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
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;
}