DEV Community

Ahumuza Ceasar
Ahumuza Ceasar

Posted on

Beginning JavaScript: Part 3

The if…else
The if else executes a block of code if a certain condition is true. If a the condition is false, then another block of code is executed. Think of it in terms of a voting process. If you are 18 and above, you are allowed to vote and if you are below 18, you aren’t allowed to vote.
We could apply the same logic in programming and execute different programs depending on certain conditions:

Image description

// The expected outcome her is Eligible to vote since age is greater than 18. If the age was less, the outcome would be not eligible to vote.

else if…
Suppose in our voting process we would want people below 18 to vote only if they have an identity card? We could check for multiple conditions using the else if block.

Image description

The age here is less than 18 but the user has an Identity card, so he/she would be eligible to vote.

Thank you for reading. I am Ahumuza Julius Ceasar. I am passionate about teaching newbies about programming. I hope you will love this.
Follow me on twitter @ahumuza_ceasar on twitter for more lessons about JavaScript

Top comments (0)