Curriculum
If Statement in JavaScript is one of the most important conditional statements used to make decisions in programming. The If Statement allows JavaScript programs to execute specific code only when a condition becomes true. Understanding the If Statement helps beginners build logical programs, create dynamic applications, and control program flow in web development.
In programming, applications often need to make decisions.
Examples:
The If Statement helps JavaScript decide whether a block of code should run or not.
The condition inside an If Statement always returns:
truefalseIf the condition is true:
If the condition is false:
Basic syntax:
if(condition){
// code to execute
}
Explanation:
if is the keywordcondition is checkedExample:
let age = 20;
if(age >= 18){
console.log("Eligible to vote");
}
Output:
Eligible to vote
Because:
20 >= 18 is trueConditions are expressions that produce:
truefalseExample conditions:
10 > 5
Result:
true
Example:
5 > 10
Result:
false
The If Statement depends on these boolean results.
Example:
let marks = Number(prompt("Enter Marks"));
if(marks >= 40){
console.log("Pass");
}
If the user enters:
50Output:
Pass
This creates dynamic decision-making programs.
Comparison operators are commonly used inside If Statements.
| Operator | Meaning |
|---|---|
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
== |
Equal to |
=== |
Strict equal |
Example:
let number = 100;
if(number === 100){
console.log("Correct Number");
}
Output:
Correct Number
An If Statement can execute multiple lines of code.
Example:
let isLoggedIn = true;
if(isLoggedIn){
console.log("Welcome User");
console.log("Dashboard Opened");
}
Output:
Welcome User
Dashboard Opened
All statements execute because the condition is true.
Boolean variables work directly with If Statements.
Example:
let isAdmin = true;
if(isAdmin){
console.log("Access Granted");
}
Output:
Access Granted
This is commonly used in authentication systems.
If Statements are used in:
Almost every software application uses conditional logic.
An If Statement can exist inside another If Statement.
Example:
let age = 25;
let hasLicense = true;
if(age >= 18){
if(hasLicense){
console.log("Can Drive");
}
}
Output:
Can Drive
Nested If Statements help manage complex conditions.
Beginners often:
Incorrect example:
let age = 20;
if(age = 18){
console.log("Eligible");
}
This uses assignment instead of comparison.
Correct example:
let age = 20;
if(age >= 18){
console.log("Eligible");
}
Understanding the If Statement helps developers:
Conditional statements are fundamental in programming.
Best practices include:
Readable conditional logic improves software quality.
The If Statement only executes code when the condition is true.
Example:
The If Else Statement provides an alternative block for false conditions.
This topic will be covered in the next lesson.
If Statement in JavaScript allows programs to execute code based on conditions. It is widely used for decision-making, validation, authentication, and dynamic application behavior. Understanding the If Statement is essential for building logical and interactive JavaScript applications.
An If Statement executes code only when a specified condition is true.
The condition returns either true or false.
Yes, If Statements can execute multiple statements inside curly braces.
If Statements are used in login systems, form validation, banking apps, and many other applications.
If executes code only when true, while If Else also handles false conditions.
WhatsApp us