Curriculum
If Else Statement in JavaScript is a conditional statement used to execute one block of code when a condition is true and another block of code when the condition is false. Understanding the If Else Statement is important for beginners because it helps developers build logical programs, decision-making systems, and interactive web applications using JavaScript.
In programming, applications often need to handle both:
The If Else Statement helps JavaScript execute different code blocks depending on the result of a condition.
Example situations:
The If Else Statement makes applications dynamic and intelligent.
Basic syntax:
if(condition){
// code if condition is true
}
else{
// code if condition is false
}
Explanation:
if checks the conditionelse executes when the condition becomes falseOnly one block executes at a time.
Example:
let age = 16;
if(age >= 18){
console.log("Eligible to Vote");
}
else{
console.log("Not Eligible to Vote");
}
Output:
Not Eligible to Vote
Because:
16 >= 18 is falseelse block executesThe If Else Statement controls the flow of execution.
Execution process:
This creates decision-making logic inside applications.
Example:
let marks = Number(prompt("Enter Marks"));
if(marks >= 40){
console.log("Pass");
}
else{
console.log("Fail");
}
If user enters:
35Output:
Fail
This makes applications interactive.
Comparison operators are commonly used with If Else Statements.
| Operator | Meaning |
|---|---|
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
== |
Equal to |
=== |
Strict equal |
Example:
let password = "admin123";
if(password === "admin123"){
console.log("Login Successful");
}
else{
console.log("Wrong Password");
}
Output:
Login Successful
Boolean variables work directly with If Else Statements.
Example:
let isMember = false;
if(isMember){
console.log("Access Granted");
}
else{
console.log("Access Denied");
}
Output:
Access Denied
This logic is commonly used in authentication systems.
Both If and Else blocks can contain multiple statements.
Example:
let isLoggedIn = true;
if(isLoggedIn){
console.log("Welcome User");
console.log("Dashboard Loaded");
}
else{
console.log("Please Login");
}
Output:
Welcome User
Dashboard Loaded
If Else Statements are used in:
Almost every software application uses If Else logic.
An If Else Statement can exist inside another conditional statement.
Example:
let age = 20;
let hasID = true;
if(age >= 18){
if(hasID){
console.log("Entry Allowed");
}
else{
console.log("ID Required");
}
}
else{
console.log("Under Age");
}
Output:
Entry Allowed
Nested conditions help manage complex application logic.
Beginners often:
Incorrect example:
let age = 18;
if(age = 18){
console.log("Eligible");
}
Correct example:
let age = 18;
if(age === 18){
console.log("Eligible");
}
Understanding comparison operators is important for correct logic.
| Statement | Purpose |
|---|---|
| If Statement | Executes code only if condition is true |
| If Else Statement | Executes one block for true and another for false |
If Else Statements provide complete decision-making control.
Understanding If Else Statement helps developers:
Conditional logic is one of the foundations of programming.
Best practices include:
Readable conditional code improves maintainability.
If Else Statement in JavaScript helps programs make decisions by executing different blocks of code for true and false conditions. It is widely used in authentication systems, validations, user interactions, and modern web applications.
An If Else Statement executes one block of code if the condition is true and another block if the condition is false.
It helps developers create decision-making and logical applications.
Yes, both If and Else blocks can contain multiple statements.
Comparison operators like >, <, ==, and === are commonly used.
They are used in login systems, banking apps, form validation, and user authentication systems.
WhatsApp us