If Else in JavaScript
Introduction to If Else in JavaScript
If else in JavaScript is used to make decisions in a program. In this JavaScript tutorial for beginners, you will learn how if else in JavaScript helps control the flow of execution based on conditions. Understanding if else in JavaScript is essential for building logical and dynamic applications.
If you want to continue learning step by step, you can click here for free courses and explore the complete JavaScript tutorial.
What is If Else in JavaScript
If else in JavaScript is a conditional statement that executes different blocks of code based on a condition. If the condition is true, one block runs; otherwise, another block runs.
Basic Syntax of If Else in JavaScript
// code runs if condition is true
} else {
// code runs if condition is false
}
Example of If Else in JavaScript
if (age >= 18) {
console.log(“You can vote”);
} else {
console.log(“You cannot vote”);
}
In this example, if else in JavaScript checks the age and prints the result.
Else If Ladder in JavaScript
The else if statement is used when you have multiple conditions.
Example of Else If in JavaScript
if (marks >= 90) {
console.log(“Grade A”);
} else if (marks >= 60) {
console.log(“Grade B”);
} else {
console.log(“Grade C”);
}
This structure helps evaluate multiple conditions step by step.
Nested If in JavaScript
You can also use if statements inside another if statement.
Example of Nested If
let hasID = true;
if (age >= 18) {
if (hasID) {
console.log(“Entry allowed”);
}
}
Nested if statements allow more complex decision-making.
Why If Else in JavaScript is Important
If else in JavaScript helps control the program flow. It allows developers to execute code based on conditions, making applications smart and interactive.
Conclusion
If else in JavaScript is a core concept for beginners. It helps in decision-making and controlling how a program behaves. By mastering if else in JavaScript, you can build more advanced logic in your applications.
FAQs
What is if else in JavaScript
If else in JavaScript is used to execute different code based on a condition.
What is else if in JavaScript
Else if is used to check multiple conditions in sequence.
Why use if else in JavaScript
If else helps in decision-making and building logical programs.



