- Learn how to write if statements
- Add consecutive conditional statements with else if
- Use the else statement when all other conditions are false
And what are conditions? Conditions can be logical operators or comparison operators, or a combination of both. Anything that returns a boolean value, including variables that are set to boolean values and using the code that's currently on the screen, I'm gonna begin with a comparison operator. First, I'm gonna clear this out and I'm gonna make the condition number one is greater than number two. So this function is gonna take two values represented by the parameters number one and number two, and compare them. And depending on the outcome of that comparison, some specific code is gonna execute. And inside of the curly brackets, I'm gonna set message equal to number one is greater than number two.
After the if statement, I'm gonna return the message that way the current value of message is gonna be made available when this function is executed. At the bottom, I'm gonna console log the compared numbers function, passing in the arguments of both the number one and number two variables that are at the top. And in the console, we can see the message number one is greater than number two, demonstrating that the current value of message was set because the comparison operator evaluated to true. If the condition evaluated to false failing, the statement would have never executed and message would have been left undefined.
Now, what if we wanted to check for another condition, if the first condition is false? We can chain additional checks by using the else/if statement. Else/if number one, triple equals number two, and inside of the curly brackets, I'm gonna set message equal to both numbers are equal. And now I'm gonna set the value of the number two variable to 15 to ensure that this condition triggers. I'm gonna execute this again and in the console, we can see the message that both numbers are equal.
So if the first condition fails, as long as there is a else if statement, the interpreter will continue to check for any additional conditions that may pass and then execute the statement for that passing condition. And you can chain multiple else/if statements, one after another, for each condition you wish to check. The only rule is that the first condition must be wrapped with the eight if statement.
Now, what if all conditions fail? That's where the else statement is used. So now I'm gonna type else, currently brackets, message equal number two is greater than number one. Know that with the else statement, there is no condition and it completely relies on the failure of all previous conditions. And I'm gonna set number two to 20. And when I execute this again, we can see in the console that number two is greater than number one. And that's it. Thanks for watching at Cloud Academy.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.