Making decisions: if and switch
Programs need to make choices: show a login button or a profile picture, accept a payment or reject it. Conditionals run different code depending on whether something is true.
if, else if, else
JavaScript checks each condition in order and runs only the first block that matches. Change temperature and run it again.
Combining conditions
The ternary operator
For a simple "this or that" choice, the ternary operator condition ? a : b fits on one line.
switch
When you compare one value against many options, switch can be cleaner than a long if/else chain.
Don't forget
break. Without it, JavaScript "falls through" and runs the next case too. Sometimes that's useful (see'sat'and'sun'above), but usually it's a bug.
Guard clauses
Pros often check for bad cases first and exit early. This keeps code flat and readable.
(Functions are covered properly in a later lesson. For now, notice how each return exits immediately.)
Practice: grade calculator
Print a grade from a marks value: 90+ → A, 75+ → B, 50+ → C, otherwise → F. Also print "Invalid" if marks are below 0 or above 100.
1.With x = 15, what does this print? if (x > 10) log('A') else if (x > 5) log('B')
2.What happens if you forget break in a switch case?