Learning JavaScript, Day 8 – Switches, Booleans, and Logic
First day of May, the sun is shining, the laundry is on the line, there are herbs to be planted, it's time to keep your appointment with the Wicker Man, and I...am learning JavaScript.
I do it to myself, and that's what really hurts.
The JavaScript Switch Statement
So apparently, switch is more readable than having a bunch of if and else statements. Which I can understand, because, yeah, the more complicated your JavaScript is, the more complicated that becomes. And the example they use explains it nicely.
let day;
let date = new Date().getDay();
switch (date) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
document.getElementById("demo").innerHTML =
"Today is " + day;
I also like how they bring in getDay which returns the day of the week as a number, starting with "Sunday = 0".
So, theoretically, I could take the above, and go:
let day;
let date = new Date().getDay();
date++;
switch (date) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
document.getElementById("demo").innerHTML =
"Tomorrow is " + day;
Which does work, and gives me Tomorrow is Saturday, but I'm not entirely sure exactly how it's working. Like, I see that date++ is incrementally adding 1 to whatever day it is, and since it's Friday (5), it's turning date into 6 (Saturday). But I'm not exactly sure how just putting date++ in there actually switches it.
But that isn't about switch, that's unrelated. I need to focus again.
I need to break my train of thought.
Eh? Eh? Get it? Because I'm learning about switch and break is what you put into your cases in order for it to move on to the next condition.
It also shows default which they're like "this is optional" and I'm going "No, no it is not, you need to have something there in case it all breaks on your stupid ass."
But, okay, let's see...switch...
let text;
let mayDay = 01052026;
switch (mayDay == 01052026) {
default:
text = "Sumer is a-cumen in...";
break;
case true:
text = "It is time to keep your appointment
with the Wicker Man.";
}
document.getElementById("demo").innerHTML = text;
So if it's May Day, it's time to keep your appointment with the Wicker Man. But if it isn't May Day, you start singing "Sumer is a-cumen in".
I'm sure there's a way to have it automatically generate it by date, but right now, I'm only seeing new Date() as the very long Fri May 01 2026 12:42:58 GMT+0100 (British Summer Time) and I'm not entirely sure how to put that into a switch statement to easily go "Yes, today is May 1st, it's time for some wicker."
I can also set it up so that multiple cases use the same block of code, which is good for when you just want it to say the same thing.
So, like...
let text;
let summerisle = new Date().getDay();
switch (summerisle) {
case 5:
text = "It is time to keep your
appointment with the Wicker Man.";
break;
case 0:
case 6:
text = "Sumer is a-cumen in";
break;
default:
text = "Where is Rowan Morrison?";
}
document.getElementById("demo").innerHTML = text;
If it's a Friday (5), it's time for the Wicker Man. If I put summerisle++; in, it's time to sing. And if I put summerisle--;, we look for Rowan.
I do need to remember to include the : at the end of each case and default, though. That got me on the exercises.
JavaScript Booleans
Booleans are pretty straightforward, really. True and false. Got it.
They're used a lot in loops, which means sense, because if you're like "if this is true, then do this".
The Boolean() function is kinda neat, and that explains why up in the switch there's (). Got it.
The only thing that might throw me is that 0 is false. I totally get that everything that has a value is true, and everything that doesn't is false, but that 0 feels like a value to me, so I'm going "no, that exists, that's true".
What's weird is that they then go "You can set Booleans to be objects!" and gives you an example, but then there's a gigantic warning going "Do not create Boolean objects!" and...maybe it's just me, but I would've put that in before showing you how to do it?
JavaScript Logical Operators
So now we go through the boolean expressions and start using them to make even more complicated logic.
&&, ||, and ! make sense, although you end up saying "true" a lot when you're explaining them and that can get confusing, but, naw, it makes sense.
?? is a little weird, but I can see why you'd use it. If someone hasn't entered in a value for the first operand, you need something there, and the next operand can be a good default.
Like they show in the example:
let name = null;
let text = "missing";
let result = name ?? text;
Someone doesn't fill in their name, they get a "missing".
The exercises throw me by asking for a ternary operator, which I have forgotten, so it takes me a few tries to remember:
var age = n;
var voteable = (age < 18) ? "Too young" : "Old enough";
alert(voteable);
That dang ?. Oh well, I'll remember it with a lot more practice.
Day 8 — Results
switchchecks an expression, and, depending on whichcaseit matches to, executes different chunks of code.breakstops the execution of code once it hits the matching expression.breakneeds to be in each case except for the last one.defaultis an "if all else fails, do this" block of code.- Cases can use the same blocks of code.
- If multiple cases have the correct value, the first one is selected.
switchcompares both value and type, so0is not the same as"0".- Don't forget the
:at the end of eachcaseanddefault. - 'Boolean()' gives you true or false depending on what's in the
(). - You can also just use the
(). 0isfalse. It has no value.&&isANDand checks if both values are true, and returnstrueif both are true.||isORand checks if either is true, and returnstrueif one is true.!isNOTand checks if both values are not true, and returnstrueif both are not true.??returns the first operand if there's something there, and returns the second operand if the first is undeclared or null.
Next week, it's all about loops. I'm going to make some programs! I'm going to Get Things Done.
Right now, though, as I have said. I have an appointment with the Wicker Man.

Today's Sticker

Like I'm not going to get out my Weird Walk Wicker Man stickers again. On this day.
Alas, they're not just out of stock, they're actually gone from the website now. Which is a shame. What's also a shame is that these Lord Summerisle "Influencer" stickers are also sold out.
Oh Lord. Oh Jesus Christ.