Learning JavaScript, Day 6 – Comparisons and Conditionals
When I was a kid, I was in Southern California, so, obviously, I learned to swim and I spent a lot of time at random pools.
I was also allergic to chlorine. Well, something in the pool chemicals, because I'd sneeze and my eyes would get itchy and I'd have a bit of a miserable time, especially if they just shocked the pool. Constantly sneezing.
And because I'm still slightly allergic, when my hay fever is already a bit nuts, being in a pool will make me sniffly and my eyes will sting like a mofo.
I mention this, because today, I am attempting to learn JavaScript while my eyes are half-closed, stinging and tired, because the chlorine in the pool during aqua aerobics, I swear to god.
JavaScript Comparison Operators
Comparing values to each other, all pretty straightforward. Stuff I've seen before and need to remember, like how == is "equal to" and === is "equal value and equal type".
So, like, if x = 2, then x == "2" will return true, but x === "2" will return false because while it's still 2 that's a string instead of a number.
All of the comparison operators can be used on strings as well as numbers, but when you do > and <, it looks at it alphabetically.
So if you were to be like:
let avatar1 = "Aang";
let avatar2 = "Korra";
let result = avatar1 < avatar2;
document.getElementById("demo").innerHTML =
"Is " + avatar1 + " less than " + avatar2 + ": " + result;
You'd get:
Is Aang less than Korra: true
And if you were to do > instead...
Is Aang greater than Korra: false
It then says that it also does it alphabetically when faced with number strings, but the example it gives doesn't make sense.
The example is:
let text1 = "20";
let text2 = "5";
let result = text1 < text2;
document.getElementById("demo").innerHTML =
"Is 20 less than 5? " + result;
Which returns:
Is 20 less than 5? true
But that can't be right, because "twenty" is T and "five" is F. So is it that it depends on what the first number is inside the string? Because that first number is a 2?
That must be the case, because when I try:
let text1 = "200";
let text2 = "1000";
let result = text1 < text2;
document.getElementById("demo").innerHTML =
"Is " + text1 + " less than " + text2 + ": " + result;
I get:
Is 200 less than 1000: false
And when I turn the variables into numbers, I get:
Is 200 less than 1000: true
That is definitely interesting and something I'll need to remember.
It then says if I compare a string to a number, JavaScript will convert the string into a number. If the value can't be converted into a number (like if it's a word), then it'll always be false.
So:
let age1 = 2;
let age2 = "2";
let age3 = "Two";
let result1 = age1 <= age2;
let result2 = age2 <= age3;
let result3 = age1 <= age3;
document.getElementById("demo").innerHTML =
age1 + " <= " + age2 +" " + result1 + "<br>"
+ age2 + " <= " + age3 + " " + result2 + "<br>"
+ age1 + " <= " + age3 + " " + result3;
Gets me:
2 <= 2 true
2 <= Two true
2 <= Two false
That is interesting. It still doesn't quite make sense to me, but I think I need to start really playing with it in a larger program to really get it.
Conditional Statements
I don't know entirely why this is under JavaScript Operators when the next section is a great big JavaScript If Else section, but, hey, whatever.
There's if, there's else, there's else if, and then there's switch. There's also (? :) which is apparently shorthand for if...else, but that one I am definitely going to need to see used in an example to get.
It then shows off the syntax, but not as examples. So I get:
if (condition) {
// code to execute if the condition is true
}
Rather than any actual code.
Which is fine when it's if, else, or else if, because that's all pretty straightforward, but then they do it for switch and the (? :), and...yeah. that loses me quickly.
Like, look at this:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
What does that meaaaaaaan...
And then:
condition ? expression1 : expression2
What. What is that.
I guess I'll find out tomorrow, when I tackle the great big JavaScript If Else section.
Day 6 — Results
==is "equal to".===is "equal value and type".!=is "not equal to".!===is "not equal value or equal type".>is "greater than".<is "lesser than".>=is "greater than or equal to".<=is "lesser than or equal to".- All of these can be used on strings as well as numbers.
- Strings will be compared alphabetically, except if they're numbers inside of strings, at which point they'll be compared against the first number in the string.
- If you compare a number to a string that can't be converted into a number, that string becomes
NaN, so then everything will befalse. ifspecifies a code block to be used if the conditional is true.elsespecifies a code block to be used if the conditional forifis false.else ifspecifies a new condition to test if the conditional forifis false.switchspecifies many alternative code blocks to use.(? :)is a shorthand forif...else.
That's it for JavaScript Operators, so I'm glad I got through that. But I am also so tired and my eyes are stinging, so I'm hoping that tomorrow's If Else section makes more sense.
Today's Sticker

It's another Club Shifty Sticker Club special. A cute little Slowpoke.
Which is also how I feel. Slowwwwwwww.