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
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.