Javascript Theory
Conditional statements in JavaScript:
If Statement
If Statement
The body of the code to be executed when the if condition is true, If the condition is false it won't to be execute the block of code.
Syntax |
---|
if (condition) { //Code to be executed. } |
If Else Statement
If Else Statement
The if-else statement is also used to test the conditions.If the condition is true, then if block is executed.If the condition is false, then the else block is executed.
Syntax |
---|
if (condition) { //Code to be executed if condition is true } else { //Code to be executed if condition is false } |
If Else If Statement
If Else If Statement
The else..if statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using else..if statement. If any one of the if condition is true, the statement related with that if is executed, and the remainder of the ladder is avoided. If none of the condition is true, then the final else statement will be executed.
Syntax |
---|
if (condition 1) { //Code to be executed if condition 1 is true } else if (condition 2) { //Code to be executed if condition 2 is true } else { //code to be executed if all the conditions return false } |
Nested If Else Statement
Nested If Statement
The nested if statement means when there is an if statement inside another if statement When an if..else statement is present inside the body of another “if” or “else” then this is called nested if else.
Syntax |
---|
if (condition 1) { //Statement(s) if(condition 2) { //Statement(s) } else { //Statement(s) } } else { //Statement(s) if(condition 3) { //Statement(s) } else { //Statement(s) } } |