Computers only function by logical rules: whether something is true or not determines if an instructions gets executed or not. This logical process is expressed in a conditional statement and goes like this: if this is the case, then that happens. Or in code:
if (/* this is the case */) {
// then this will happen
}A condition is put in the ( ) and it needs to evaluate to true or false (also known as Boolean values). If the condition is true, then whatever is inside the { } will be executed.
What happens when the condition is false? For that we have the else { } block. If the condition is false, then whatever is inside the else will be executed:
if() {
} else {
}The following video shows different ways of telling JavaScript to go in a different direction:
{% hyf-youtube src="https://www.youtube.com/watch?v=nBj2nJup8xU" %}
If you just can't get enough, here are some extra links that mentors/students have found useful concerning this topic: