JavaScript is a popular programming language used for creating interactive web pages. One of the most important features of JavaScript is the ability to make decisions based on certain conditions. This is where the If Else statement comes into play. The If Else statement is used to execute a block of code if a certain condition is true, and another block of code if the condition is false.
The If Else statement is a conditional statement that allows you to execute different blocks of code based on whether a certain condition is true or false. The basic syntax of the If Else statement is as follows:
if (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false }
The condition in the If Else statement can be any expression that evaluates to either true or false. If the condition is true, the code inside the first block of curly braces will be executed. If the condition is false, the code inside the second block of curly braces will be executed.
Here is an example of how the If Else statement can be used in JavaScript:
var age = 18; if (age >= 18) { console.log("You are old enough to vote."); } else { console.log("You are not old enough to vote."); }
In this example, the condition is whether the variable age is greater than or equal to 18. If the condition is true, the message "You are old enough to vote." will be printed to the console. If the condition is false, the message "You are not old enough to vote." will be printed instead.
Here are some more examples of how the If Else statement can be used in JavaScript:
var x = 10; if (x > 5) { document.write("x is greater than 5."); } else { document.write("x is less than or equal to 5."); }
In this example, the condition is whether the variable x is greater than 5. If the condition is true, the message "x is greater than 5." will be written to the document. If the condition is false, the message "x is less than or equal to 5." will be written instead.
var time = new Date().getHours(); if (time < 12) { document.write("Good morning!"); } else if (time < 18) { document.write("Good afternoon!"); } else { document.write("Good evening!"); }
In this example, the condition is based on the current time of day. If the time is before 12pm, the message "Good morning!" will be written to the document. If the time is between 12pm and 6pm, the message "Good afternoon!" will be written. If the time is after 6pm, the message "Good evening!" will be written instead.
var grade = "B"; if (grade == "A") { document.write("Excellent!"); } else if (grade == "B") { document.write("Good job!"); } else if (grade == "C") { document.write("You can do better."); } else { document.write("You failed."); }
In this example, the condition is based on a student's grade. If the grade is an A, the message "Excellent!" will be written to the document. If the grade is a B, the message "Good job!" will be written. If the grade is a C, the message "You can do better." will be written. If the grade is anything else, the message "You failed." will be written instead.
The If Else statement is a powerful tool in JavaScript that allows you to make decisions based on certain conditions. By using the If Else statement, you can create more dynamic and interactive web pages that respond to user input and other events. With practice, you can become proficient in using the If Else statement and other JavaScript features to create amazing web applications.