If Else Statement in JavaScript

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.
- if statement
- if...else statement
- 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
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.
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.
×
![]()































































