'if', 'if-else', 'if-else if' statements
Introduction
In JavaScript, conditional statements are used to perform different actions based on different conditions. These statements allow the program to make decisions and execute different code blocks based on the outcome of those decisions. There are three main types of conditional statements in JavaScript: `if`, `if else`, and `if else if`.
The `if` Statement
The `if` statement is used to execute a block of code if a specified condition is true. Here is the syntax for the `if` statement:
if (condition) { // code to be executed if the condition is true }
The `condition` can be any JavaScript expression that returns a boolean value (`true` or `false`). If the condition evaluates to `true`, the code block within the curly braces will be executed. Otherwise, the code block will be skipped.
Example:
let age = 18; if (age >= 18) { console.log("You are old enough to vote"); }
In this example, if the `age` variable is greater than or equal to 18, the message "You are old enough to vote" will be printed to the console.
The `if else` Statement
The `if else` statement allows you to execute different code blocks based on the outcome of a condition. If the condition specified in the `if` statement evaluates to `true`, the code block within the `if` block will be executed. Otherwise, the code block within the `else` block will be executed. Here is the syntax for the `if else` statement:
if (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false }
Example:
let age = 16; if (age >= 18) { console.log("You are old enough to vote"); } else { console.log("You are too young to vote"); }
In this example, if the `age` variable is greater than or equal to 18, the message "You are old enough to vote" will be printed to the console. Otherwise, the message "You are too young to vote" will be printed.
The `if else if` Statement
The `if else if` statement allows you to test multiple conditions and execute different code blocks based on the outcome of those conditions. You can have any number of `else if` blocks between the `if` and `else` blocks. Here is the syntax for the `if else if` statement:
if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true } else { // code to be executed if none of the conditions are true }
The conditions are evaluated in order. If the first condition (`condition1`) is true, the code block within its associated block will be executed. If `condition1` is false, but `condition2` is true, the code block within the `else if` block will be executed. If none of the conditions are true, the code block within the `else` block will be executed.
Example:
let grade = 75; if (grade >= 90) { console.log("You got an A"); } else if (grade >= 80) { console.log("You got a B"); } else if (grade >= 70) { console.log("You got a C"); } else { console.log("You got an F"); }
In this example, depending on the value of the `grade` variable, a different message will be printed to the console. If the `grade` is 75, the message "You got a C" will be printed.
Key Takeaways
- Conditional statements in JavaScript allow you to make decisions and execute different code blocks based on conditions.
- The `if` statement is used to execute a code block if a condition is true.
- The `if else` statement is used to execute one code block if a condition is true, and another code block if the condition is false.
- The `if else if` statement allows you to test multiple conditions and execute different code blocks based on the outcome of those conditions.
Comments
Post a Comment