Conditional Statement in JS — with examples
We have different types of conditional statement in JS, which we can use according to the requirement and logic. Okay, so let’s start and understand them with examples.
Table of Contents
What is Conditional Statements?
A conditional statement is an essential part of any programming language. It is used to execute one or more statements based on a particular condition. Each condition works on a Boolean value that can be true or false.
How many types of Conditional Statements in JS?
3. else if
So if-else and switch-case both allow us to make these decisions based on a condition. Let’s discuss them one by one.
if-else statement takes a specific condition and checks whether the condition is true or false. If the condition is true, then the if statement executes a specific code block.
Let’s take an example, in the below piece of code, the age is 15 which is less than 18 so the condition is true the output will be executed.
In the above example, when I changed the age to 20, then the 1st condition is not met in that case i.e the condition is false, then the else statement executes a different code.
If you want to read the full post, then reach out at here SidTechTalks. I have explained in details. I hope you like it, and if you like it please share with your friends.