Detailed Article About JavaScript else if Statement
JavaScript else if and if-else both are conditional statements are used to do different tasks based on a given condition.
Basically, JavaScript has multiple types of conditional statements which we use according to our task at the time of web development. There is 4 type of conditional statement in JavaScript.
Introduction to JavaScript else if Statement
else if Statement mainly executes the block of code when specified if condition returns false.
Syntax of JS else if Statement
if (expression 1) {
Statement(s) to be executed if expression 1 is true
} else if (expression 2) {
Statement(s) to be executed if expression 2 is true and expression 1 is false
} else if (expression 3) {
Statement(s) to be executed if expression 3 is true and expression 2 is false
} else {
Statement(s) to be executed if no expression is true
}
Javascript Conditional Statements
At the time of doing the code, we execute JS conditional statements based on the given conditions.
Here are some of the statements are which we use,
Example of JS else if Statement :
var x = 40;
if (x > 50) {
/* do something */
} else if (x > 5) {
/* do something */
} else {
/* do something */
}
Code Explanation:
Live Example of Else if and If else Statement in JavaScript
https://codepen.io/phpcodertech/pen/YzVMvyM
In the above example, we create a program to check the language string in if and else if condition.
Where we first define a variable and set the string value. Then we check the language with == double equal operator to check both are equal or not.
If the condition is not satisfied then it goes to else if condition.
As in our example if the statement condition is false and it goes to else if block where it returns true and prints the value on the web page.
I hope you understand all the things, please let me know if you facing any issues.
Also Read: jQuery Signature Pad with saving As Image Using html2canvas
Happy Coding..!