JavaScript's "If-Else"​ Decisions

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.


To view or add a comment, sign in

More articles by Danika Hannon

  • My Nine Year Journey into Cyber Security

    I’ve had a longstanding interest in cyber security that’s spanned nine years. The first time that cyber security landed…

    69 Comments
  • Minnesota's Quantum Impact

    Over the next five to 10 years, quantum computing will have major impacts on a wide range of industries, including…

    5 Comments
  • Max Out Your Training

    When COVID-19 hit, I traded running at the gym for running laps around my neighborhood. Changing environments made me…

    8 Comments
  • MinneQuantum & the QED-C

    MinneQuantum has great news to share, we've joined the Quantum Economic Development Consortium (QED-C)! We're excited…

    3 Comments
  • Strategy Mining

    The upside of data science is that it can be applied to almost any subject matter. The downside? That can get…

  • Answer the Call

    When I started moving into tech, I caught on that there's a wealth of coding challenges. But the one competition that's…

  • Building Your Own Data Set

    Lately I've been making a data set for a machine learning project and, since this is the first data set I've made…

    6 Comments
  • Connecting to Your Community

    In light of the recent events across the nation, if you're feeling pulled to give back to your community (but aren't…

  • Feature Engineering Saved My Project

    Does anyone ever feel like feature engineering should have hero status in the data science world? I certainly do…

  • Learning Resources for the Long-Run

    Because of COVID-19, quite a few organizations have given out free access to their learning tools. While that’s great…

    3 Comments

Insights from the community

Others also viewed

Explore topics