Introduction to JavaScript elseIf
JavaScript elseIf is a Conditional Statement for Decision making in programming used to control the flow of execution of the lines of Code based on certain specified conditions. If the condition is satisfied, the statement returns true and an action is performed else if the condition fails, another operation is performed. Javascript elseIf statement is used to cause the flow of execution to branch based on changes to the lines of the program.
Basically, if is used to specify a block of code to be executed if the condition is true. Need to use else if the specific block of code to be executed if the condition if false. Here we use else if to specify the new conditions to be tested if the previous condition is false.
We have various conditional statements to perform different decisions,
- if: Specifying block of code to be executed if the condition is true
- else: Specifying block of code to be executed if the condition is false
- else if: Specifying new condition to be tested, if the first condition fails
- switch: Specifying alternative blocks of code to be executed
Syntax:
if ( condition1 ) {
//first block of code, will be executed if condition is true
} else if ( condition2 ) {
//code block executed if condition1 fails and condition2 is true
} else {
//code block to be executed if condition1 and condition2 is false
}
Examples of JavaScript elseIf
We shall see some examples with which you will get a clear idea of the use of else if:
Example #1
Code:
Using JavaScript else If condition
Enter a number and Get your gift Here!!
Output:
So you need to enter any random number, based on the condition, text will be displayed.
Based on the if condition, and else if conditions, you can see the result above.
Example #2
Code:
Using JavaScript else If condition
Output:
Changing the employee value to ‘Juhi’, you can see that else condition has passed.
Example #3
Code:
Using JavaScript else If condition
Output:
Am changing the value to 100, and here is the output,
Example #4
Code:
Using JavaScript else IF condition
Click Here to get Greeting based on the Time
Output:
After clicking on the button
Note: Executed at the time of 24:05, hence its Good Afternoon!!
Example #5
Code:
Using JavaScript else If condition
Output:
You can check with any other numbers.
Conclusion
With this we can conclude our topic ‘JavaScript elseIf’. We have seen this simple topic with few of the examples listed above. I hope you have understood those examples. Giving a good try with the above examples itself is enough to understand the Conditional Statements and their use. You can even take the help of Debug mode in your editor which will show the values that are being taken.
Recommended Articles
This is a guide to JavaScript elseIf. Here we discuss the introduction and syntax of javascript elseif along with different examples and its code implementation. You may also have a look at the following articles to learn more –
- JavaScript Animation
- JavaScript Convert to JSON
- JavaScript Array Contain
- JavaScript onkeyup
The post JavaScript elseIf appeared first on EDUCBA.
This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here