Learning if ... else and switch statement commands in JavaScript
6 minute(s) read | Published on: Jul 14, 2021 Updated on: Dec 14, 2021 |
JavaScript is very popular among programmers, and they get a lot of help from this language to create different programs. The reason for its great popularity is that there are various features in this language that can be used to achieve the main goal so that it causes the user to have a better user experience while using the program or site which is created by using this language, as we have mentioned above, in this program, there are various possibilities that can be used by users to help them achieve the desired results in various fields. As a result, it is necessary to review the points related to each one so that you can get help from each of them in the right place and time.
In the following, we will review two of the features that exist that you can use to improve the quality of the content, but before that, we need to give a brief explanation about JavaScript.
What is a JavaScript programming language?
It is one of the programming languages through which the content of a web can be converted from static to dynamic, which enables the user to interact more with the web and programs which are created with the help of this language.
If ... else in JS:
The if / else statement causes a block of code to be executed if a predetermined condition is specified. If another condition is not met, another code set can be executed.
In this language, we have the following conditional statements:
- Use if:
You can use it when the condition you set is correct, and after checking that condition, the code can be executed.
- Use else:
You can use else to specify a block of code to be executed if the same condition is incorrect.
- Use else if:
It is necessary to use it when the first condition you have set is incorrect, and it is necessary to execute the block of code after checking the second condition.
- Use switch:
It can be used when there are many blocks of code, and you need to select a block from all of them to be executed.
To understand more about if ... else in JS, you can consider the following example.
The result of the example, if the condition is met, after clicking the "Try it" button, will be as follows.
Click the button to display a time-based greeting.
Try it
Good day
But if the first condition, which has been set, is not met and the conditions are different, the result will be as follows.
Click the button to display a time-based greeting.
Try it
Good evening
Note the following example in which three conditions are set, that if they are not met, the other condition will be considered.
The result of the above example, after clicking on the specified button, in a case that the first condition is not met, but the second condition is met, will be as follows.
Click the button to get a time-based greeting.
Try it
Good day
Imagine that you want to use this feature in this language to interact more with users who are using the content which is produced by you; to understand it better, consider the following example.
The above example will be as follows if the user enters the letter C inside the specified box.
I'm thinking of a letter. Guess which: a, b, c, d, or e?
c
Try it
Spot on! Good job!
If any letter is entered, a block of code will be executed according to the condition which is set at the time of coding, that can be used on different sites to perform various tasks that allow the user to interact more with the content.
JavaScript switch Statement:
This is used when you want to execute a block of code among a large number of them that exist, the switch statement is used to perform various actions based on different conditions, and when there are long expressions, the best possible result can be gained by using it.
In simple terms, about this case, the switch statement evaluates an expression and compares each of the existing states with the value of the expression after performing the evaluation, and finally, if each of these cases matches each other, the relevant code will be executed, note the following.
This statement is often used together with a break or a default keyword, or with both of them, the break keyword breaks out of the switch block, which will stop the execution of more execution of code or case testing inside the block; if the break is omitted, the next code block in the switch statement will be executed, the default keyword specifies some code to run if there is no case match. There can only be one default keyword in a switch. However, this is optional; it is recommended that you use it because it takes care of unexpected cases; now consider the following example:
By clicking on the specified button, it will show you what day it is; for example, if you click on the button on Monday, the result will be as follows.
Click the button to display what day it is today.
Try it
Today is Monday
This feature in this programming language can help a lot to increase users' interaction with content and site because you can use it to show reactions to users' answers so that if the user answers, it is possible to show a different reaction to that answer, now you can see an example in the following in this regard.
The result of the example will be as follows, for instance, if the answer given by the user is Daiquiri.
Click the button to display a dialog box that will ask for your favorite drink.
Try it
Daiquiri is my favorite too!
Last word:
In general, there are various possibilities in this programming language, which we have discussed in this article about if ... else and switch Statement. By giving different examples, we tried to help you understand more about these cases, so it can be concluded how much does the use of each item affects the user interaction and cause them to have more interaction with the content.
Click to analyze your wesbite SEO