These Are My Conditions

Cory McLeanCory McLean
4 min read

So some of you last time said "Cory you're not destructively removing the last element of an array, but you stay ready to .pop( ) off" and to that, I say you're right!


Today we're doing the one about conditionals and I swear I'm not writing this just so I have something to reference when I forget how to do them. Even though I'm still a student, I 100% know what I'm talking about so buckle up friends.

Conditionals are kind of like your girlfriend who can never make up her mind. She says stuff like “If we get McDonald's then I want a Big Mac, but if we get Popeye’s then I want a chicken sandwich.” But at the end of it all, she always wants some of the fries you ordered that she swears she didn’t need.

They're useful if you want to “make something happen” depending on another thing (a condition). There are two types of conditionals you can use in JS (that's cool kid talk for JavaScript), the first being the if statement and the second being the switch statement let's start with the if statement and its Robin then see if we can work our way over to the switch statement.


The IF statement

The if statement outputs code based on if its condition has been met, it looks like this:

if(condition) {do this}

The if statement has three parts though, and each part allows us to add more to it. The second part adds that something else should happen if the condition has not been met. That looks like this :

if(condition) {do this} else{do this}

The last part lets us add multiple conditions by adding more than one if and more than one else:

if(condition) {do this} else if(condition2) {do this} else if(condition3) {do this} else{"I'm gonna give you this right here"}

Now I'd like to call on the help of an R&B legend to help us practice this statement. He will show us how the if statement can be used practically if not very seriously, ladies and gentlemen Mr. Brian Mcknight:

... he's not actually here but this replit holds one of his most influential songs. I'm sure we all know how it goes: one ...something about a dream, and two ... I think he's stalking someone at that point. The fact of the matter is that there is a function named brianSays in this replit and it takes any integer and outputs code based on what the integer is. Now, I'm not going to tell you which integers Brian used (you can go on genius for that) but I will tell you that calling a function looks like this:

brianSays()

When we call a function we want to pass an argument. We put the argument in the parenthesis and in this case, all of our arguments should be integers. Give it a try down below and then use the show code button to select index.js so you can see how this works using the if statement


The Ternary Operator

If the if statement is Nightwing then the ternary operator would be Robin (threw you for a loop there didn't I?). The ternary operator is a shorthand for the if statement. It lets us output a code based on a condition. The way it works is by testing the condition set forth and returning something if it tests true and returning something else if it tests false. The ternary operator looks like this:

condition ? code to run if true : code to run if false

Every single if so far has been brought to you by today's sponsor: My Tidal Playlist; If it thumps and makes you want to shake something, it's 100% My Tidal Playlist™ that's 29 including the ones in this sponsor message


Anyway, that was the first type of conditional in JS. Now I know I said we'd get to the switch statement but how could I not capitalize on the title pun and break this into two parts? So as the great usher once said "I'm so throwed and I don't know what to do but to give you part two of my conditions" ...he kind of said that ...Bye.

-♥C

0
Subscribe to my newsletter

Read articles from Cory McLean directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Cory McLean
Cory McLean