java scripts conditional statement
In programming languages, you will have a set of conditional statements. These help you to make decisions in your program. In easy terms, let’s say you are interacting with a chatbot. Now, based on your inputs/commands, that chatbot will respond. At the software level, the chatbot makes decisions as per the inputs it receives. These decisions are made using conditional statements. Regardless of the type of programming language, every conditional statement returns a true or false boolean value. In this guide, we will look at the conditions statements of JaveScript. You can also enhance your programming skills with the help of the examples listed below.

Type of Conditional Statements in JavaScript

Conditional Statements are almost similar in all programming languages. Since we are talking about JS, there are three different types of conditional statements available in JavaScript:
  • If Statement
  • If-else Statement
  • If-else if Statement

If Statement

If you want to make only one decision, you can use the if statement. The If statement will only allow you to write a code block for the True boolean value. Syntax:

if(condition)
{
   // type code here, need to be executed if the condition is true
}
Example,

var rating = 4;

if(rating<10)
{
   console.log("Rating value less than 10");
}
Output:
Rating value less than 10
 

If else Statement

In the If else statement, you can add the else statement along with the if statement. You can execute a code block if the condition doesn’t return true using this. Therefore, the else statement will be executed when the condition returns false. Syntax:

if(condition)
{
   // type code here, need to be executed if the condition is true
}
else
{
   // type code here, need to be executed if the condition is false
}
Example,

var rating = 8;

if(rating<5)
{
   console.log("Rating value less than 5");
}
else
{
    console.log("Rating value greater than 5");
}
Output:
Rating value greater than 5
  We have stored integer 8 in the variable age in the above code. Therefore while checking the condition age>18, it has returned false, and that’s why the else code block was executed.

If else-if Statement

You can use the if else-if statement when you want to test multiple conditions (at least more than 2). Here you can use multiple else-if code blocks after the first if condition. Syntax:

if(condition 1)
{
   // type code here, need to be executed if the condition 1 is true
}
else if(condition 2)
{
    // type code here, need to be executed if the condition 1 is false and condition 2 true
}
else
{
    // type code here, need to be executed if the condition 1 and condition 2 false
}
Note: After the else-if code block, it is mandatory to place the else code block. In technical terms, the else code block is referred to as the default statement, and in case none of the if or else if code blocks get executed, the else block will be executed. Refer to the example below for a better understanding:

var rating = 15;

if(rating >20)
{
   console.log("Rating value greater than 20");
}
else if(rating<20 & rating > 10)
{
   console.log("Rating value greater than10 and less than 20");
}
else
{
   console.log("Rating value less than 10");
}
Output:
Rating value greater than10 and less than 20
  We hope now you have a better understanding of the JavaScript conditional statements. Most programming languages have similar conditional statements; therefore, while learning new languages, you won’t have to invest your time in going through conditional statements. These statements might seem too easy, but there are billions of situations where these are widely used in the real world. You must practice them thoroughly. If you have any doubt, do let us know in the comments, and we will discuss them further. Do spread this guide if you find it helpful.
Leave a Reply