
TL;DR
- In your applications, the control flow is the one in charge of making sure your code is taking the right path.
- if, else if, and else help run different code depending on conditions.
- Youāll usually combine control flow with comparison or logical operators (coming up in the next article!).
- Good control flow helps avoid messy, repetitive code.
Getting started
Writing code is basically telling the computer what to do, one step at a time. But what if you have to decide something based on the data that will change the steps that the computer needs to take? How do you tell the computer, āhey, when this happens, do this or thatā? That is exactly where control flow comes into play. It makes sure your code is able to decide what to do based on certain conditions.
In this article, Iāll walk you through the most basic building blocks for making decisions in JavaScript: if, else if, and else.
š”Before we jump in, these statements work hand-in-hand with operators, like ==, >, or &&. If those look a little fuzzy or you are finding them hard to understand right now, donāt worry, weāre covering operators in the next article.
What Is Control Flow?
As I explained above, control flow is but a fancy term for how your code āflowsā from one process to the next.
By default, JavaScript runs code from top to bottom, line by line, but using conditional statements, we can essentially interrupt, modify, and even reset that flow by tell our program :
- āIf this is true, do this.ā
- āOtherwise, maybe do that.ā
- āIf none, do this other thing instead.ā
The if statement
The if statement is, pretty self explanatory IMO, is just stating IF something happens, then do something, look at it in this way:
if (condition) {
// code to run if condition is true
}Now letās say we are trying to check IF someone passed their test, the minimum approving score is 70
ā
let score = 90;
// Passes only if trictly greater than 70 (70 fails)
if (score > 69) {
console.log("Not bad.");
}
// Passes if it is higher or equal to 70 (70 passes)
if (score >= 70) {
console.log("Not bad.");
}Here, JavaScript checks if score is larger than 70 it prints āNot bad.ā If not, it skips it.
The else if Ā and else statements
Now, what do we do when we need multiple conditions? As the title might suggest, we use the else if and else statements, which I believe to be pretty self-explanatory:
if (score > 80) {
console.log("Nice job!");
} else if (score > 70) {
console.log("Not bad.");
} else {
console.log("Keep practicing.");
}This lets your code pick between several paths, or a fallback if no path applies. JavaScript will stop checking once it finds a match.
Pro Tips to keep your conditions clean
- Keep conditions simple, if they get too messy, consider breaking them into variables.
- You CAN nest if statements, but this doesnāt mean you should. Having conditions going too deep makes your code hard to read and hard to understand.
- Always test all branches of your conditions.
Coming next: Operators in JavaScript
In the next part, weāll dig into how JavaScript manages operators. Weāll then see how they pair with if, if else, and else to create proper data flows
Control Flows In a Nutshell

ā


