Comparisons using <
, >
, ==
, ===
, !=
, !==
, <=
, and >=
have been discussed a lot in the previous notes. Here's the summary.
null == undefined
and they don't equal to anything elsenull
or undefined
as they have specific behaviours.Check out the complete reference here: JavaScript: Comparison
if
else if
else
When it's more like conditional inclusions and exclusions, use if
system.
switch
When it's more like options, especially when there are many, use switch
.
if (condition 1) {
code block 1;
} else if (condition 2) {
code block 2;
} else {
code block x;
}
It reads:
We can have more than have one else if
.
Note that only exclusion from previous condition goes to the next. When a condition is met, its code block is run, then it exits.
Conditions can be as many as needed. But in the case there are a lot of conditions (especially options type), consider using switch
instead of if
that's more like a list than inclusion-exclusion conditions.
today = new Date(); time = today.getHours(); if (time < 12) { console.log("Good morning!"); } else if (time < 17) { console.log("Good afternoon!"); } else { console.log("Good evening!"); }
switch (variableName) {
case value1:
code block A;
break;
case value2:
code block B;
break;
case value3:
case value4:
code block C;
break;
default:
code block Z;
}
Switch statement
break
break
statement is used to stop execution inside the switch block.default
default
case is for when there is no match.break
.today = new Date(); month = today.getMonth(); monthName; switch (month) { case 0: monthName = "January"; break; case 1: monthName = "February"; break; case 2: monthName = "March"; break; case 3: monthName = "April"; break; case 4: monthName = "May"; break; case 5: monthName = "June"; break; case 6: monthName = "July"; break; case 7: monthName = "August"; break; case 8: monthName = "September"; break; case 9: monthName = "October"; break; case 10: monthName = "November"; break; case 11: monthName = "December"; } dateName = `${today.getDate()} ${monthName} ${today.getFullYear()}` console.log(`Today is ${dateName}.`);
Without break
statement:
month = 1; let correctMonth; let forgetSomeBreaks; let forgetAllBreaks; switch (month) { case 0: correctMonth = "January"; break; case 1: correctMonth = "February"; break; case 2: correctMonth = "March"; break; default: correctMonth = "December"; } switch (month) { case 0: forgetSomeBreaks = "January"; case 1: forgetSomeBreaks = "February"; case 2: forgetSomeBreaks = "March"; break; default: forgetSomeBreaks = "December"; } switch (month) { case 0: forgetAllBreaks = "January"; case 1: forgetAllBreaks = "February"; case 2: forgetAllBreaks = "March"; default: forgetAllBreaks = "December"; } console.log("Correct month: " + correctMonth); console.log("Forget some breaks: " + forgetSomeBreaks); console.log("Forget all breaks: " + forgetAllBreaks); /* Correct month: January Forget first break: February Forget all breaks: December */
When the case
is in range format, set the switch's paranthesis value into true: switch (true)
and range can be safely written in each case's parenthesis: case (varName <= 4)
.
(condition) ?
(if true, run this code) :
(if false, run this code)
In ternary, we are giving 1 condition. If it's true, run the left side code. If it's false, run the right side code.
let greeting = (isBirthday) ? "Happy Birthday" : "Good morning";
<label for="theme">Select theme: </label> <select id="theme"> <option value="light">Light theme</option> <option value="dark">Dark theme</option> </select> <script> const userTheme = document.querySelector('#theme'); const userHtml = document.querySelector('html'); function update(bgColor, textColor) { userHtml.style.backgroundColor = bgColor; userHtml.style.color = textColor; } theme.addEventListener('change', () => ( userTheme.value === 'dark' ) ? update('black','white') : update('white','black')); </script>