Hello friend,today I'm going to share you what i learn about conditional statement in Javascript.
Some primary conditional statements in Javascript include:
- if statement
- if...else statement
- if...else if...else statement
*1.if statement *
This statement executes a block of code only if a specified condition is true.
if (condition) {
// Code to be executed if condition is true
}
2.if...else statement
This statement executes one block of code if the condition is true and another block of code if the condition is false.
if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
3.if...else if...else statement
This statement allows for checking multiple conditions sequentially. If the first if condition is false, it proceeds to check the else if conditions, and if none of the if or else if conditions are true, the else block is executed.
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 above conditions are true
}
Conclusion
To understanding this type of conditional statement is very simple when you can think it's logical to solve the problem or code as easy.So first understand the logic and then give the condition.
Top comments (0)