30 Days Of JavaScript

Day 3: Logic

Lesson 4: If Else

Let's go back to our first example today:

You may have thought at the time:

Isn't it a shame we had to write the case for true and false with if both times 🤷‍♂️

Well, if you did I have good news. if has a counterpart called else that can handle that false case in the example above. We can rewrite the above as:

I've also removed the === true since happyCoder is already a boolean.

The else { } block doesn't need a condition with ( ) because it will run when the if ( ) condition is false.

if statements don't need an else after them but else statements always need a preceding if - after all, there can't be an else unless there isn't an alternative to begin with!

Outline

Go Pro?

Upgrade to Pro for quizzes, tracked progress and a completion certificate for just $25 🚀

Want more developer tips, tricks and tools?
Then follow me:
FREE Coding Career Quick Start Guide 🚀
Discover the best way to learn to code, how to land a job and valuable resources to help your journey in this free 15 page value packed guide.
Looking to email me? You can get me on my first name at allthecode.co