Conditional statements
Conditional statements are programming constructs that allow programmers to make decisions and execute different blocks of code based on a certain condition. There are three main types of conditional statements: if-else statements, switch statements, and ternary operators.
If-else statements: An if-else statement is used to execute a block of code if a certain condition is true, and a different block of code if the condition is false. The general syntax of an if-else statement is as follows:
Switch statements: A switch statement is used to execute different blocks of code based on the value of a variable or expression. The general syntax of a switch statement is as follows:
Ternary operators: A ternary operator is a shorthand way of writing an if-else statement that evaluates a single expression and returns one of two possible values depending on whether the condition is true or false. The general syntax of a ternary operator is as follows:
If the condition is true, the operator returns value1; otherwise, it returns value2.
Conditional statements are essential in programming because they allow programs to make decisions and take different actions based on different inputs or conditions.
Here are some examples of conditional statements in JavaScript:
If statement:
The if statement is used to execute a block of code only if a specific condition is true.
Example:
In this example, if the age
variable is greater than or equal to 18, the message "You are eligible to vote" will be displayed in the console.
If-else statement:
The if-else statement is used to execute a block of code if a specific condition is true and another block of code if the condition is false.
Example:
In this example, if the num
variable is divisible by 2, the message "5 is an odd number" will be displayed in the console.
If-else-if statement:
The if-else-if statement is used to execute different blocks of code for different conditions.
Example:
In this example, if the grade
variable is greater than or equal to 90, the message "You got an A" will be displayed in the console. If the grade
is between 80 and 89, the message "You got a B" will be displayed, and so on.
Switch Statement Example:
In this example, we have a switch statement that checks the value of the variable day
. Depending on the value of day
, it will execute the corresponding code block. If day
is "Monday", it will print "Today is Monday" to the console. If day
is "Tuesday", it will print "Today is Tuesday", and so on. If day
doesn't match any of the cases, it will execute the default
case and print "Today is some other day".
Ternary Operator Example:
In this example, we have a ternary operator that checks if the variable age
is greater than or equal to 18. If it is, it will set the value of the variable message
to "You are an adult". If it's not, it will set the value of message
to "You are not an adult". The ternary operator is a shorthand way of writing an if-else
statement.
These are just a few examples of conditional statements in JavaScript. There are many other types of conditional statements, which can be used to create more complex logic in your code.
Last updated