Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is a conditional statement in JavaScript

Understanding Conditional Statements

A conditional statement is like a crossroads in your code. It tells your computer, "If this condition is true, take this path. If it's not, take the other path." In everyday life, we make these kinds of decisions all the time. For example, "If it's sunny outside, I'll go for a walk. If it's not, I'll stay in and read." In JavaScript, we use conditional statements to make these kinds of decisions in our code.

The Basic Structure of a Conditional Statement

A conditional statement in JavaScript has a specific structure. It begins with the keyword if, followed by a condition enclosed in parentheses. This is followed by a block of code enclosed in braces. Here's what it looks like:

if (condition) {
  // code to run if the condition is true
}

Imagine you have a pet dog and you want to feed it. But you only want to feed it if it's hungry. In JavaScript, you could write this decision as a conditional statement like this:

let isDogHungry = true;

if (isDogHungry) {
  console.log("Feeding the dog");
}

In this code, isDogHungry is the condition. If it's true (which it is), we run the code inside the braces, which in this case is console.log("Feeding the dog").

The Else Clause

But what if the dog isn't hungry? We don't want to feed it then. This is where the else clause comes in. The else clause is code that runs when the condition in the if statement is false. Here's how we'd add an else clause to our dog-feeding code:

let isDogHungry = false;

if (isDogHungry) {
  console.log("Feeding the dog");
} else {
  console.log("The dog is not hungry");
}

In this code, isDogHungry is false. So instead of running the code in the if block, we run the code in the else block.

The Else If Clause

Sometimes, we have more than two options. For these situations, JavaScript gives us the else if clause. The else if clause is a way to check multiple conditions. If the first condition is false, it checks the second one, and so on.

Let's say we have a traffic light. It can be red, green, or yellow. We could model this with an else if statement like this:

let trafficLightColor = "yellow";

if (trafficLightColor === "red") {
  console.log("Stop");
} else if (trafficLightColor === "green") {
  console.log("Go");
} else if (trafficLightColor === "yellow") {
  console.log("Slow down");
} else {
  console.log("Invalid color");
}

Here, we're checking multiple conditions. If the traffic light is red, we stop. If it's green, we go. If it's yellow, we slow down. And if it's any other color (which should never happen!), we print a message saying it's an invalid color.

Conclusion

In the realm of programming, conditional statements are like the nerve center, guiding the flow of execution based on various conditions, akin to how our brain makes decisions based on the information it receives.

As a new programmer, mastering conditional statements is like learning to steer your vehicle, giving you control over where your code should go and what it should do. Start by understanding the simple if statement, then venture into the else and else if territory. The journey might seem full of conditions, but remember, every condition is a step closer to your destination – becoming an adept programmer. Keep coding, keep exploring!