If else Statement in JavaScript, If else condition in JavaScript,javascript conditional operator, javascript if else shorthand, different conditional statements, javascript if statement multiple conditions, javascript complex if statement, how to add multiple conditions in if statement javascript
If else Statement in JavaScript, If else condition in JavaScript,javascript conditional operator, javascript if else shorthand, different conditional statements, javascript if statement multiple conditions, javascript complex if statement, how to add multiple conditions in if statement javascript
Hello everyone, I hope you are having a fine day. In today’s tutorial, I am going to discuss the if-else statement in
JavaScript  and what are conditional statements in detail. In programming, while writing a program there may be an occasion where you need to adopt one out of a given set of paths. In such a case, you need to use conditional statements that allow your program to make the correct decision.

Conditional statements 

Conditional statements are used to run a specific action based on the result of the different conditions (true or false). If the condition would be true one action would be performed and if the condition is false then another action would be performed. A few examples of JavaScript conditional statements:
  • Verify the location of the user and display the language based on the country
  • Send a form on submit, display a missing required field warning next in the missing field.
  • Show a booking form for a bus ticket but not if the bus is booked.
We have the following conditions in JavaScripts.
  1. if statement
  2. if...else statement 
  3. if...else if statement

If Statement in Javascript

If statement plays a very important role in JavaScript to make decisions. It will decide whether the statement is true or false and only run if the statement is true. Otherwise, the code block will be ignored if the statement is false. 
Syntax if (condition) { // block of code that will excute if the condition is true }
Note: if is a lowercase letter and If or IF will cause a JavaScript error. A simple example to see how if statement work in javascript. Let’s suppose you are going to pay your electricity bill through an app where you have a certain amount of money already in your account.
If else Statement in JavaScript, If else condition in JavaScript,javascript conditional operator, javascript if else shorthand, different conditional statements, javascript if statement multiple conditions, javascript complex if statement, how to add multiple conditions in if statement javascript
Your account money is 5000 and you have to pay the electricity bill of 700. Using the <= help you in finding that whether you have less than or equal to electricity bill money available to pay it. Since the Electricity_bill <= money evaluates to true. The code will run because the condition is true. output
If else Statement in JavaScript, If else condition in JavaScript,javascript conditional operator, javascript if else shorthand, different conditional statements, javascript if statement multiple conditions, javascript complex if statement, how to add multiple conditions in if statement javascript
See another example, in case you have less money than your Electricity_bill.
If else Statement in JavaScript, If else condition in JavaScript,javascript conditional operator, javascript if else shorthand, different conditional statements, javascript if statement multiple conditions, javascript complex if statement, how to add multiple conditions in if statement javascript
This example does not have any output because of Electricity_bill <= money evaluated to false. Now the code block will be ignored and the programme will continue on the next line. And there will be nothing in output.

If Else Statement in JavaScript

In if statement code only proceeds when the condition is true but in the if-else statement whether the condition evaluates true or false it will execute.
Syntax if (condition){ // block of code that will execute if the condition is true } else { block of code that will execute if the condition is false }
As an example, to find out an even or odd number.
Output
If else Statement in JavaScript, If else condition in JavaScript,javascript conditional operator, javascript if else shorthand, different conditional statements, javascript if statement multiple conditions, javascript complex if statement, how to add multiple conditions in if statement javascript
Since the condition of if is not true then code moves to the else statement and execute it.  if-else condition is more common than only if condition. It can help users in giving warning and letting them know what decision they should make. 

IF Else If Statment in JavaScript

If you want to check more than two conditions than you can use if...else if statement. With if...else statement, you can find out that if the condition is true or false. However, sometimes we have multiple possible conditions and outputs and needed more than two options. For that purpose, we can use if...else if statement.
Syntax if(condition a) { // block of code that will execute if condition a is true } else if (condition b) { // block of code that will execute if condition b is true } else if (condition c) { // block of code code that will execute if condition c is true } else { // block code that will execute if all above conditions are false }
There is no limit for else if statement, you can add as many as you want but there is a switch statement that would be preferred for readability. Let's see a simple example of checking grade of students based on numbers out of 100.
If else Statement in JavaScript, If else condition in JavaScript,javascript conditional operator, javascript if else shorthand, different conditional statements, javascript if statement multiple conditions, javascript complex if statement, how to add multiple conditions in if statement javascript
  Output
If else Statement in JavaScript, If else condition in JavaScript,javascript conditional operator, javascript if else shorthand, different conditional statements, javascript if statement multiple conditions, javascript complex if statement, how to add multiple conditions in if statement javascript
This tutorial provided a brief overview of conditional statement, if, if..else and if...else if statement that you can use in JavaScript. To learn more about JavaScript from the first article you can read  Introduction to JavaScript. Thank you for visiting our website. You have any question regarding this post feel free to comment below and ask any question related to this post. We are looking forward to hearing from you.