• danikahannon

JavaScript's "If-Else" Decisions



As I continue my adventure with JavaScript, I've been charmed by "if-else" decisions.


How They Work


As I learned from Codecademy:

"... if-else decisions can be modeled in code by creating conditional statements. A conditional statement checks specific condition(s) and performs a task based on the condition(s)."

Here's one example of how they work:


The code's saying that if the condition (in this case, the condition's "sale") evaluates to "true," the code inside the brackets ("Time to buy!") will print to the console on the right.


That's a good start, but you may've noticed the "else" is missing. Let's see what happens when it's added:


Even though "else" was added, that statement didn't execute because the condition didn't evaluate to "false."


So let's change change "let sale = true;" to "let sale = false;" and see what happens:


Pretty neat, right?


This time the code executed "Time to wait for a sale," because the condition evaluated to "false".


That was Technical . . . 


So let's look at that logic another way:


The process flow shows how the code evaluates and decides what to run.


"If-Else" Statements in Daily Life


I thought about how this logic could be used in daily life and I realized the logic's great for scheduling things.


Once that clicked, I couldn't resist adding that logic to my weekly schedule.


Now on my Sunday list of to-dos, there's a line that says, "if laundry needs to be done, do laundry. Else play Legend of Zelda." (I'm a big fan of the Legend of Zelda video games.)


What's Fun for You? 


If you're learning a new skill, have you found anything that's surprised you, too?

Let me know if you have! It'd be great to hear from you and learn how your journey's going.


In the meantime, I'll be looking forward to Sunday . . . and hoping that laundry's done.