JavaScript(JS) if...else Statement
JavaScript if Statement
In JavaScript, the if
statement is a conditional statement that allows you to execute a block of code only if a certain condition is true. The basic syntax of the if
statement is as follows:
if (condition) { // code to execute if condition is true }Source:www.theitroad.com
Here, condition
is an expression that is evaluated to either true
or false
. If the condition is true
, the block of code inside the curly braces is executed. If the condition is false
, the block of code is skipped.
For example, let's say we have a variable num
and we want to check if it is greater than 10
. We can use the if
statement to conditionally execute a block of code based on this condition:
const num = 15; if (num > 10) { console.log("The number is greater than 10"); }
In this example, the if
statement checks whether the value of num
is greater than 10
. Since num
is 15
, which is greater than 10
, the block of code inside the curly braces is executed and the message "The number is greater than 10" is logged to the console.
You can also use the else
clause to specify a block of code to execute if the condition is false. The basic syntax for this is:
if (condition) { // code to execute if condition is true } else { // code to execute if condition is false }
For example, let's say we have a variable num
and we want to check if it is even or odd. We can use the if
statement with an else
clause to conditionally execute different blocks of code based on whether the condition is true or false:
const num = 6; if (num % 2 === 0) { console.log("The number is even"); } else { console.log("The number is odd"); }
In this example, the if
statement checks whether the value of num
is even or odd by using the modulo operator (%
) to check if num
is divisible by 2
. Since num
is 6
, which is divisible by 2
, the condition is true and the message "The number is even" is logged to the console.
JavaScript if...else statement
In JavaScript, the if...else
statement is a conditional statement that allows you to execute one block of code if a condition is true, and another block of code if the condition is false. The basic syntax of the if...else
statement is as follows:
if (condition) { // code to execute if condition is true } else { // code to execute if condition is false }
Here, condition
is an expression that is evaluated to either true
or false
. If the condition is true
, the block of code inside the first set of curly braces is executed. If the condition is false
, the block of code inside the second set of curly braces is executed.
For example, let's say we have a variable age
and we want to check if it is greater than or equal to 18
. We can use the if...else
statement to conditionally execute different blocks of code based on this condition:
const age = 20; if (age >= 18) { console.log("You are an adult"); } else { console.log("You are not an adult yet"); }
In this example, the if
statement checks whether the value of age
is greater than or equal to 18
. Since age
is 20
, which is greater than or equal to 18
, the block of code inside the first set of curly braces is executed and the message "You are an adult" is logged to the console.
You can also use multiple else if
clauses to check for multiple conditions. The basic syntax for this is:
if (condition1) { // code to execute if condition1 is true } else if (condition2) { // code to execute if condition2 is true } else { // code to execute if all conditions are false }
For example, let's say we have a variable score
and we want to assign a letter grade based on the score. We can use the if...else if...else
statement to conditionally execute different blocks of code based on the score:
const score = 80; if (score >= 90) { console.log("A"); } else if (score >= 80) { console.log("B"); } else if (score >= 70) { console.log("C"); } else if (score >= 60) { console.log("D"); } else { console.log("F"); }
In this example, the if...else if...else
statement checks the score against multiple conditions and assigns a letter grade accordingly. Since score
is 80
, which is greater than or equal to 80
but less than 90
, the block of code inside the second else if
statement is executed and the message "B" is logged to the console.
JavaScript if...else if statement
In JavaScript, the if...else if
statement is used when you want to execute different code blocks depending on multiple conditions. It allows you to test for multiple conditions and execute different blocks of code for each condition.
The basic syntax of the if...else if
statement is as follows:
if (condition1) { // code to execute if condition1 is true } else if (condition2) { // code to execute if condition2 is true } else if (condition3) { // code to execute if condition3 is true } else { // code to execute if all conditions are false }
Here, condition1
, condition2
, and condition3
are expressions that are evaluated to either true
or false
. The if
statement is evaluated first, and if the condition is true, the corresponding code block is executed. If the condition is false, the else if
statement is evaluated, and so on.
For example, let's say we have a variable score
and we want to assign a letter grade based on the score. We can use the if...else if
statement to conditionally execute different blocks of code based on the score:
const score = 80; if (score >= 90) { console.log("A"); } else if (score >= 80) { console.log("B"); } else if (score >= 70) { console.log("C"); } else if (score >= 60) { console.log("D"); } else { console.log("F"); }
In this example, the if...else if
statement checks the score against multiple conditions and assigns a letter grade accordingly. Since score
is 80
, which is greater than or equal to 80
but less than 90
, the block of code inside the second else if
statement is executed and the message "B" is logged to the console.
You can also use nested if...else if
statements to check for more complex conditions. However, be careful not to make the code too complicated and hard to read.
Nested if...else Statement
In JavaScript, a nested if...else
statement is used when you want to check for multiple conditions, where the second condition depends on the first condition being true. This allows you to create more complex logic in your code.
The basic syntax of a nested if...else
statement is:
if (condition1) { // Code to execute if condition1 is true if (condition2) { // Code to execute if both condition1 and condition2 are true } else { // Code to execute if condition1 is true and condition2 is false } } else { // Code to execute if condition1 is false }
Here, condition1
and condition2
are expressions that evaluate to either true
or false
. If condition1
is true
, the first block of code is executed. If condition2
is also true
, the nested block of code is executed. If condition2
is false
, the code in the else
block is executed.
For example, let's say we want to check if a given number is positive, negative, or zero. We can use a nested if...else
statement to accomplish this:
const num = 10; if (num > 0) { console.log("Positive"); } else if (num < 0) { console.log("Negative"); } else { if (num === 0) { console.log("Zero"); } else { console.log("Not a number"); } }
In this example, the outer if...else
statement checks if the number is positive or negative. If the number is neither positive nor negative (i.e., zero or not a number), we use a nested if...else
statement to check for the additional condition.
Note that you can have as many nested if...else
statements as needed to create the desired logic, but be careful not to make the code too complicated and hard to read.