JavaScript

How to Use If Statements in JavaScript

How to Use If Statements in JavaScript

JavaScript is one of the most popular programming languages in the world, and for good reason! It’s versatile, easy to learn, and plays a huge role in making websites interactive. If you’re just starting your journey into programming with JavaScript, understanding control structures like if statements is a must.

In this blog post, we’ll break down how to use if statements in JavaScript and explain them in simple terms, so you can easily grasp their functionality. Whether you’re creating an interactive web page or a small app, knowing how to implement conditions using if statements will make your JavaScript code more powerful and flexible. Let’s dive in!

What is an If Statement

At its core, an if statement is a way for a program to make decisions. It’s like a fork in the road: depending on whether a certain condition is true or false, your code will take one path or the other.

In JavaScript, the basic syntax of an if statement looks like this:

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

Here’s the breakdown:

  • condition: A statement that evaluates to either true or false. For example, checking if a number is greater than another number.
  • The code inside the curly braces {} will only run if the condition evaluates to true. If the condition is false, the code inside the block is ignored.

Basic Example of an If Statement

Let’s start with a simple example to make things clearer. Suppose you want to check if a number is greater than 10:

let number = 15;

if (number > 10) {
  console.log("The number is greater than 10.");
}

In this case, the condition number > 10 is true because 15 is indeed greater than 10. As a result, the message “The number is greater than 10” will be logged to the console.

But what happens if the number is less than or equal to 10? That’s where else statements come into play.

How to Use an Else Statement

Sometimes, you might want to do something else if the condition isn’t true. This is where the else statement comes in. It allows you to specify an alternative block of code to run if the condition is false.

Here’s an example that checks if a number is greater than 10. If it is, it prints one message. If it’s not, it prints a different message.

let number = 5;

if (number > 10) {
  console.log("The number is greater than 10.");
} else {
  console.log("The number is 10 or less.");
}

In this case, since 5 is not greater than 10, the output will be: “The number is 10 or less.”

Adding Multiple Conditions with Else If

What if you want to check more than two conditions? This is where else if becomes useful. It lets you chain multiple conditions together and handle more complex decision-making.

For example, let’s check if a number is greater than 10, equal to 10, or less than 10:

let number = 10;

if (number > 10) {
  console.log("The number is greater than 10.");
} else if (number === 10) {
  console.log("The number is exactly 10.");
} else {
  console.log("The number is less than 10.");
}

Here, the condition number === 10 checks if the number is exactly 10. Since number is indeed 10, the output will be: “The number is exactly 10.”

Using Logical Operators with If Statements

Sometimes, you need to check multiple conditions at once. You can do this by using logical operators like AND (&&) and OR (||). These operators allow you to combine conditions for more flexibility.

AND Operator (&&)

The AND operator checks if both conditions are true. Here’s an example that checks if a number is between 10 and 20:

let number = 15;

if (number > 10 && number < 20) {
  console.log("The number is between 10 and 20.");
}

Since 15 is between 10 and 20, the condition is true, and the message “The number is between 10 and 20” will be logged.

OR Operator (||)

The OR operator checks if at least one of the conditions is true. Here’s an example that checks if a number is either less than 10 or greater than 20:

let number = 25;

if (number < 10 || number > 20) {
  console.log("The number is either less than 10 or greater than 20.");
}

Since 25 is greater than 20, the condition evaluates to true, and the message will be printed.

Nesting If Statements

You can also nest if statements within each other. This means you can have an if statement inside another if statement, which is helpful for checking more complex conditions.

Here’s an example of a nested if statement:

let number = 15;

if (number > 10) {
  if (number < 20) {
    console.log("The number is between 10 and 20.");
  }
}

In this example, the outer if checks if the number is greater than 10, and the inner if checks if the number is less than 20. Since both conditions are true for 15, the message will be printed.

Common Mistakes to Avoid with If Statements

As you start using if statements in your code, there are a few common mistakes that you should be aware of:

  1. Using the Assignment Operator Instead of the Equality Operator: Remember that = is for assignment, and == or === is for comparison. Using = in an if statement will lead to unexpected behavior.
    • Incorrect: if (number = 10) (this will assign 10 to number)
    • Correct: if (number == 10) or if (number === 10)
  2. Missing Curly Braces: If you only have one statement inside an if block, you can skip the curly braces. But it’s always safer to use them, especially when adding more statements later.
    • Correct: if (number > 10) { console.log("Number is greater than 10"); }

Conclusion

If statements are a foundational concept in JavaScript that allow you to control the flow of your program by making decisions based on conditions. By using if, else, else if, and logical operators, you can create powerful and dynamic applications.

author-avatar

About Rick Bowen (JavaScript)

Hi, I'm Rick! I'm an accomplished Software Engineer with broad and deep expertise in Go JavaScript, TypeScript, Shell (bash/zsh), Git, SQL & NoSQL Databases, Containers + Kubernetes, Distributed Systems, Reliability Engineering, DevOps, Cloud / Network / Application Security, Identity / Access Management, Linux, macOS/Darwin, CI/CD, SaltStack, Terraform, AWS, GCP, Azure, Internet Protocols, and much more.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments