The programming path is not always a straight line. The programmer must write the codes in such a way that they have different functions in different conditions. For this, conditional statements and commands are used. The use of conditional expressions in the code makes us have a more complete program in which various conditions are predicted and a solution is considered for it.

For example, in a game, if the number of lives of the player reaches zero, the game is over. In a weather app, a sunrise image is displayed if it’s morning, and stars and moon if it’s night.

In other words, using conditional statements, the program is able to decide what to do based on the inputs it receives. In this article, the conditional expression in JavaScript is introduced.

The if…else statement

if…else is one of the most used conditional statements in JavaScript language. The if…else structure allows the programmer to advance the code based on a certain condition. So that if the condition is true, a certain code will be executed, and if the condition is false, another code will be executed.

The if keyword marks the beginning of a conditional statement. After if, inside the parentheses, is the condition that we want to check whether it is true or false.

If the condition inside the parentheses is true, the code inside the braces after if is executed.

The else keyword is usually placed after the if block and indicates that if the condition inside the parentheses is false, the block of code inside the else braces will be executed.

The else if statement

This conditional structure allows the program to check multiple conditions and execute the corresponding code block based on the results of these checks. If a condition is executed and is true, the corresponding else if code block is executed; If none of the conditions are executed or the result of all of them is false, the code block corresponding to else is executed.

In this structure, the first condition is checked first. If this condition is true, the corresponding block is executed. But if this condition is false, the program goes to the next condition, `else if (condition2)’ and checks the second condition. This process continues until one of the conditions is true or an `else’ block is reached where none of the conditions are true.

Thus, `else if’ allows checking several different conditions and allows the programmer to more carefully consider different conditions and write the program in a more logical and dynamic way.

The expression switch…case

The conditional statement `switch…case’ is used in JavaScript to select from among several different options based on the value of a specified variable. This statement contains a control variable whose value is checked and a series of possible values (cases) that are compared with the value of that variable.

In this structure, the program first checks the value of the variable and then compares it with the case values. If the value of the control variable is the same as one of these values, the code block corresponding to that value is executed. Each case can contain one or more commands.

If the value of the control variable does not match any of the possible values, the program goes to the default block and the corresponding code is executed.

“break” command in “switch…case” command means exiting the loop. When the “break” command is used in a “case” block, the execution of the code from that block ends and the execution of the “switch” loop does not continue after that.

ternary operator

The ternary operator checks a condition and returns a value/expression if the condition is true, and returns another value/expression if the condition is false.

In other words, the performance of the ternary operator is the same as if…else, except that in this case the number of lines of code will be much less.

JavaScript conditional statements are a fundamental and vital part of the code, which help the programmer to write the code in a more organized and precise manner. By using these expressions, various conditions are checked and the program is executed based on these conditions in a logical and accurate way. In other words, the use of conditional commands leaves the hand of the programmer open for creative programming.

Leave a Reply

Your email address will not be published. Required fields are marked *