Conditional statements let you execute blocks of code based on certain conditions.
The JavaScript language offers various ways of using conditional statements. Many of them are common to other programming languages as well. However, you should know the benefits of each and how they work in JavaScript.
1. if-else and else-if statements
Ann if-else The statements execute one block if the condition is true and the other block if the condition is false. else-if executes a block that matches one of several conditions, or executes a default block if none of the conditions match.
A true value is the value that JavaScript considers truth when encountered in a boolean context.False values are values that JavaScript considers error when encountered in a boolean context.
JavaScript considers all values true unless a few values are false.false value is error, 0, -0, 0n, “”, null, undefinedand NaN.
has the following syntax: if-else statement:
if (condition) {
} else {
}
In some cases, it may be necessary to check multiple related conditions. In these scenarios, else-if Evaluate extra conditions.
for example:
if (condition) {
} else if (condition_2) {
} else if (condition_n) {
} else {
}
use else-if Statements allow you to evaluate any number of conditions. However, this method quickly becomes ugly and difficult to maintain as the number of conditions increases.
JavaScript is switch statement.
2. switch statement
of switch The statement evaluates the expression once and attempts to match it with one or more possible values. Each possible match is case keyword.
If the switch statement finds a match, it executes all subsequent statements. break down statement.
The syntax for the switch statement is:
switch (expression) {
case 'first-case':
break;
case 'case_2':
break;
default:
}
of break down The statement is switch to specify where to stop executing code. If you miss a break statement, code execution continues, executing all other code blocks after the first match. This is a rare occurrence.
3. Ternary operator
In JavaScript, you can also use the ternary operator to shorten conditional statements.
The ternary operator takes three operands:
- A question mark (?).
- After the question mark, a colon (:). This will be executed if the condition is true.
- An expression after a colon that is executed if the condition is false.
for example:
condition ? console.log('Condition is truthy') : console.log('Condition is falsy');
The above statement effectively means “if ‘condition’ is true, then log the first message, else log the second message”.
4. Short circuit
Short-circuiting is a technique that involves using logical operators again (||) and and (&&) evaluates expressions from left to right.
Operations involving the OR operator short-circuit by returning the first true value encountered. If all values of the expression are false, short-circuit and return the last false value.
Operations with the AND operator short-circuit by returning the first bogus statement found. Short-circuits and returns the last true value if all statements in the expression are true.
An example of a conditional statement using the OR operator is shown below.
app.listen(process.env.PORT || 3000)
This shorthand approach to creating conditional statements is common in Express applications. It is written as: port Environment variables exist. Please use it. Otherwise, use port 3000.
An example of a conditional statement using the AND operator is shown below.
foo && console.log('foo is defined')
The code block above is an “if Phew Calls the console.log() function, if defined.
This technique is the shortest way to write a conditional statement, but it can make your code less readable. Avoid overuse, especially if you are working as part of a large team.
Importance of conditionals
Conditional statements are what allow the program to make decisions. Without them the code runs straight from start to finish. They are also part of the loop. Without them the loop would run infinitely and crash the application.