You have dealt with JavaScript and want to understand the basics of conditions? Conditions are fundamental to programming, as they allow you to make decisions and shape your code dynamically. Whether you are writing a simple script or a complex application, understanding if conditions is essential. In this guide, you will learn how to effectively use conditional statements.
Key takeaways
- An if statement checks whether a condition is met.
- You can combine multiple conditions with else if and else.
- Comparison operators and logical operators help to check complex conditions.
Step-by-Step Guide
1. The if Statement
The basic construct to check a condition in JavaScript is the if statement. It looks like this: if (condition) { code }. If the condition is true, the code inside the curly braces is executed.

2. Using Variables
To demonstrate the power of the if statement, let’s look at a variable. Suppose you have defined the variables x and y. You could use the following condition to check if x is less than y.
3. Adding an else branch
To perform an alternative action when the condition is not met, you can use the else branch. This should follow directly after your if statement.
In this case, the second console.log would be executed since x is not less than y.

4. else if for Multiple Conditions
If you want to check multiple possible conditions, you can use else if. Here’s an example showing what that looks like:
This construct is useful when you have more than two possible outcomes.
5. Using Comparison Operators
Understanding the comparison operators used is crucial. The most popular ones are:
- <: less than.
- >: greater than.
- ===: equal.
- !==: not equal.
6. Logical Operators
To combine conditions, there are logical operators like && (AND) and || (OR).

7. Negation Operator
The negation operator! is used to reverse a condition.
Here, the code will be executed when x and y are not equal.
8. Conclusion and Outlook
Now that you know the basics of conditions in JavaScript, you can use them to control and make your code flexible. In the upcoming lessons, we will deal with loops, where you can apply your knowledge of conditions.
Summary – Basics of Condition with if in JavaScript
You have learned how to use conditions in JavaScript with if, else if, and else. You have also learned about comparison and logical operators that help you formulate more complex conditions.
Frequently Asked Questions
What is an if statement?An if statement checks a condition and executes code if it is met.
How can I check multiple conditions?Use else if to define additional conditions and else for the case that none of the conditions apply.
What are comparison operators?Comparison operators compare values, e.g.,, ===,!==.
What does the logical operator && do?The logical operator && checks if both conditions are true.
What does the negation operator! mean?It reverses the truth value of a condition.