Conditions are one of the most important concepts in JavaScript. They allow a program to make decisions based on different situations. Instead of executing every statement one after another, JavaScript can check whether a condition is true or false and then execute the appropriate block of code.
Almost every interactive website uses conditional statements. Login systems, registration forms, shopping carts, online quizzes, search filters, payment gateways, and many other web applications depend on conditions to determine what action should be performed.
Without conditions, every JavaScript program would always produce the same output regardless of user input. Conditional statements make programs intelligent by allowing them to respond differently to different values and situations.
A condition is an expression that evaluates to either true or false. JavaScript checks this result and decides which block of code should be executed.
For example, a website may allow a user to log in only if the entered password is correct. Similarly, an online examination portal may display a "Pass" message only when the student's marks satisfy the required criteria.
JavaScript provides several conditional statements that help developers implement decision-making logic efficiently.
Conditional statements allow developers to control the flow of a program. Instead of executing every instruction, JavaScript performs actions only when specific conditions are satisfied.
Decision making means selecting one action from multiple possible actions based on a condition.
JavaScript first evaluates the condition. If the condition becomes true, one block of code executes. Otherwise, JavaScript skips that block or executes another block depending on the program logic.
This process allows websites to react intelligently to user actions and changing data.
The if statement is the simplest conditional statement in JavaScript. It executes a block of code only when the specified condition evaluates to true.
If the condition is false, JavaScript simply skips the block and continues executing the remaining statements.
if(condition)
{
// statements
}
The working process of an if statement is simple.
let age = 20;
if(age >= 18)
{
document.write("Eligible to Vote");
}
Eligible to Vote
Since the value of age is greater than or equal to 18, the condition becomes true and the message is displayed.
let number = 15;
if(number > 0)
{
console.log("Positive Number");
}
Positive Number
let number = 12;
if(number % 2 == 0)
{
document.write("Even Number");
}
Even Number
The modulus operator returns the remainder after division. If the remainder is zero, the number is even.
Every condition produces either a true or false value. JavaScript uses these Boolean values to decide whether a code block should execute.
| Condition | Result |
|---|---|
| 10 > 5 | true |
| 8 < 3 | false |
| 15 == 15 | true |
| 20 != 20 | false |
| 7 >= 4 | true |
Comparison operators compare two values and always return either true or false.
| Operator | Description | Example |
|---|---|---|
| == | Equal to | 5 == 5 |
| === | Strict Equal | 5 === 5 |
| != | Not Equal | 5 != 3 |
| > | Greater Than | 20 > 10 |
| < | Less Than | 8 < 15 |
| >= | Greater Than or Equal | 18 >= 18 |
| <= | Less Than or Equal | 9 <= 12 |
Conditions are used everywhere in modern web applications.
The if...else statement is used when a program must choose between two possible actions. If the specified condition evaluates to true, the code inside the if block executes. Otherwise, the statements inside the else block are executed.
This is one of the most commonly used decision-making statements in JavaScript because many real-world situations require two possible outcomes.
if(condition)
{
// Executes if condition is true
}
else
{
// Executes if condition is false
}
let age = 16;
if(age >= 18)
{
document.write("You are eligible to vote.");
}
else
{
document.write("You are not eligible to vote.");
}
You are not eligible to vote.
Since the value of age is less than 18, JavaScript executes the else block.
let marks = 72;
if(marks >= 40)
{
document.write("Congratulations! You Passed.");
}
else
{
document.write("Sorry! You Failed.");
}
Congratulations! You Passed.
Sometimes there are more than two possible outcomes. In such situations, JavaScript provides the else if statement.
The else if statement allows multiple conditions to be checked one after another. As soon as one condition becomes true, JavaScript executes its corresponding block and skips the remaining conditions.
if(condition1)
{
// Block 1
}
else if(condition2)
{
// Block 2
}
else if(condition3)
{
// Block 3
}
else
{
// Default Block
}
let marks = 81;
if(marks >= 90)
{
document.write("Grade A+");
}
else if(marks >= 75)
{
document.write("Grade A");
}
else if(marks >= 60)
{
document.write("Grade B");
}
else if(marks >= 40)
{
document.write("Grade C");
}
else
{
document.write("Fail");
}
Grade A
A nested if means placing one if statement inside another if statement. Nested conditions are useful when one decision depends on another decision.
if(condition1)
{
if(condition2)
{
// Statements
}
}
let marks = 85;
let age = 18;
if(age >= 18)
{
if(marks >= 75)
{
document.write("Admission Approved");
}
}
Admission Approved
Logical operators combine two or more conditions into a single expression.
| Operator | Name | Meaning |
|---|---|---|
| && | AND | Returns true only if both conditions are true. |
| || | OR | Returns true if at least one condition is true. |
| ! | NOT | Reverses the Boolean value. |
let age = 22;
let citizen = true;
if(age >= 18 && citizen)
{
document.write("Eligible to Vote");
}
Eligible to Vote
let username = "admin";
let email = "admin@gmail.com";
if(username == "admin" || email == "admin@gmail.com")
{
document.write("Login Successful");
}
Login Successful
JavaScript automatically converts values into Boolean values while evaluating conditions.
All other values are generally considered truthy.
The switch statement is another decision-making statement in JavaScript. It is used when a program needs to compare a single value against multiple possible cases. Instead of writing many if...else if statements, the switch statement provides a cleaner and more organized solution.
The switch statement checks an expression and executes the matching case. If none of the cases match, the optional default block is executed.
switch(expression)
{
case value1:
// Statements
break;
case value2:
// Statements
break;
default:
// Statements
}
The break keyword stops the execution after the matching case is completed. Without break, JavaScript continues executing the following cases.
let day = 3;
switch(day)
{
case 1:
document.write("Monday");
break;
case 2:
document.write("Tuesday");
break;
case 3:
document.write("Wednesday");
break;
case 4:
document.write("Thursday");
break;
case 5:
document.write("Friday");
break;
default:
document.write("Weekend");
}
Wednesday
let choice = "+";
let a = 15;
let b = 5;
switch(choice)
{
case "+":
document.write(a + b);
break;
case "-":
document.write(a - b);
break;
case "*":
document.write(a * b);
break;
case "/":
document.write(a / b);
break;
default:
document.write("Invalid Operator");
}
20
The break statement immediately exits the switch block after executing the matched case. It prevents the execution of the remaining cases.
let number = 2;
switch(number)
{
case 1:
document.write("One");
break;
case 2:
document.write("Two");
break;
case 3:
document.write("Three");
break;
}
Because of the break statement, only the second case is executed.
The default block is executed when none of the specified cases match the given expression.
let color = "Yellow";
switch(color)
{
case "Red":
document.write("Stop");
break;
case "Green":
document.write("Go");
break;
default:
document.write("Unknown Color");
}
Unknown Color
| if...else | switch |
|---|---|
| Works with any condition. | Compares one expression with multiple values. |
| Best for complex conditions. | Best for fixed values. |
| Supports logical operators. | Does not directly support logical expressions. |
| Can become lengthy. | Provides cleaner code for multiple choices. |
| Suitable for ranges of values. | Suitable for exact matching values. |
Use the switch statement whenever a single variable needs to be compared with several fixed values.
Conditional statements are used in almost every modern web application. They allow websites to react differently depending on user actions and data.
JavaScript conditional statements enable programs to make decisions based on different situations. The if, if...else, else if, and switch statements help developers control program flow and create interactive web applications. Understanding these concepts is essential for building login systems, calculators, forms, games, shopping websites, and many other real-world applications. A strong understanding of conditions also prepares you for advanced JavaScript topics such as loops, functions, events, DOM manipulation, and asynchronous programming.