In JavaScript, the if/else
statement is used to make decisions in your code based on a condition. It allows you to execute one block of code if a certain condition is true and another block of code if the condition is false. Here's the basic syntax of the if/else
statement:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Here's a breakdown of how it works:
-
The
if
keyword is followed by an expression enclosed in parentheses. This expression is the condition you want to evaluate. If the condition is true, the code inside the first block (enclosed by curly braces) will be executed. If the condition is false, the code inside theelse
block (enclosed by curly braces) will be executed. -
You can omit the
else
block if you only want to execute code when the condition is true. In that case, if the condition is false, nothing will be executed.
Here's an example:
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are not an adult.");
}
In this example, if the age
is greater than or equal to 18, it will print "You are an adult." Otherwise, it will print "You are not an adult."
You can also have multiple if
conditions using else if
:
let score = 75;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B");
} else if (score >= 70) {
console.log("C");
} else {
console.log("F");
}
This code will evaluate the score
variable and print the corresponding grade based on the value of score
.
if (test_condition) {
// if statement body
}
const score = parseInt(prompt('Enter your marks:'));
if (score >= 50) {
console.log('You have passed your exams.');
console.log('Congratulations.');
}
Output
Enter your marks: 80
You have passed your exams.
Congratulations.
const score = parseInt(prompt('Enter your marks:'));
if (score >= 50) {
console.log('You have passed your exams.');
console.log('Congratulations.');
}
if (score < 50) {
console.log('Sorry, you failed your exam.');
}
Output
Enter your marks: 40
Sorry, you failed your exam.
if (condition) {
// code inside if block
}
else {
// code inside else block
}
const score = 40;
if (score >= 50) {
console.log('You have passed your exams.');
console.log('Congratulations.');
}
else {
console.log('Sorry, you failed your exam.');
}
Output
Sorry, you failed your exam.
if (test_condition1) {
// statements1
}
else if (test_condition2){
// statements2
}
else {
// statements3
}
const score = 40;
if (score > 100) {
console.log('Score is invalid');
}
else if ( score < 0) {
console.log('Score is invalid');
}
else if (score >= 50) {
console.log('You have passed your exams.');
console.log('Congratulations.');
}
else {
console.log('Sorry, you failed your exam.');
}
Output
Sorry, you failed your exam.
const score = -45;
if (score > 100) {
console.log('Score is invalid');
}
else if ( score < 0) {
console.log('Score is invalid');
}
else if (score >= 50) {
console.log('You have passed your exams.');
console.log('Congratulations.');
}
else {
console.log('Sorry, you failed your exam.');
}
Output
Score is invalid
const score = -45;
if (score > 100 || score < 0) {
console.log('Score is invalid');
}
else if (score >= 50) {
console.log('You have passed your exams.');
console.log('Congratulations.');
}
else {
console.log('Sorry, you failed your exam.');
}
Output
Score is Invalid.
Can you create a program to check whether a number is positive or negative or 0? To create this program, create a variable named number and assign a value to it based on the user input. Then using a if statement, check if the number variable is positive or negative or 0. If the number is positive, print "The number is positive." If the number is negative, print "The number is negative." (and) If the number is 0, print "The number is 0."
const number = parseInt(prompt('Enter a number:'));
if (number > 0) {
console.log("The number is positive.");
}
else if ( number < 0) {
console.log("The number is negative.");
}
else {
console.log('The number is 0.');
}
Output
Enter a number: 40
The number is positive
Q. What is the output of the following code?
const time = 15;
if (time < 10)
console.log("Good Morning");
else if (time < 19)
console.log("Good Evening");
else if (time < 22)
console.log("Good Night");
else
console.log("Invalid Time");
- Good Morning
- Good Evening
- Good Night
- Invalid Time
Answer: 2