Hello everyone, welcome back, in this module, I am going to discuss Javascript Conditional Statements. In the previous modules, we have studied what is JavaScript, and what are variables, operators, and functions in JavaScript. Now, let’s know conditional statements.
JavaScript Conditional Statements
In this module, I will discuss if and if-else statements, and in the next module, I will be discussing the switch statement.
Like other programming languages, conditional statements in JavaScript are also known as decision-making statements. Conditional statements are the statements used to execute a particular set of codes depending upon the condition.
In JavaScript, there are three types of conditional statements or decision-making statements.
- if statement
- if-else statement
- switch statement
Why use conditional statements in JavaScript?
In our daily life, we have to make some decisions and, based on the decision taken we decide what to do next or perform some task. For example, if I am hungry, only I will take dinner. Similarly, in programming sometimes a decision has to be made to perform some operation or execute statements.
if-statement in JavaScript
The if-statement in JavaScript executes the set of statements only if the given condition is true.
Syntax
if (condition){ // statements }
Example
if (age>=18){ console.log ("Eligible"); } //Eligible
Here, in the above example, the condition age>=18 is true so, we are getting Eligible as an output.
Given below is the flow chart for the if-statement to show how the if-statement executes the statements.
if-else statement in JavaScript
The if-else statement executes the if-block statements only if the given condition is true, otherwise, it executes the else-block statements.
Syntax
if (condition) { // statement1 } else { // statement2 }
Example
let age=14; if (age>=18) console.log ("Eligible"); else console.log ("Not Eligible"); //Not Eligible
Here, in the above example, the condition age>=18 is false, the else block is executed and we get a Not Eligible message as output. Given below is the flow chart for the if-statement to show how the if-statement executes the statements.
Note: If there is only one line of statement inside if-block or else-block, then we can omit curly braces ({}).
Nested if statement in JavaScript
The nested if-statement means if statements or if-else statements are placed inside other if statements or else statements. Using a nested if statement, you can create multiple conditions.
Syntax
if (condition1) { if (condition2) { // statement (s) } }
Example
if (6>3) { if (4>2) console.log ("Hello"); } //Hello
Here, in the above example, the condition age>=18 is true, we are getting Eligible as an output.
We can also use nested if-statement inside the else block.
Syntax
if(condition1) { // statement (s) } else { if (condition2) { // statement (s) } }
Example
let a=-6; if (a<0) console.log("a is less than 0."); else { if (a%2==0) console.log ("a is even."); }
We can also use another if-else statement inside if and else. For example,
let a=6; if (a<0) console.log ("a is less than 0."); else { if(a%2==0) console.log("a is even."); else console.log ("a is odd."); }
If-else-if ladder in JavaScript
We can use multiple if-else-if statements to make multiple decisions. As soon as the condition satisfies or evaluates to be true, the statement associated with the if or else if block is executed, otherwise the statement associated with the else block is executed and the remaining else-if statements are ignored or not executed.
Syntax
if(condition1){ //statement1 } else if(statement2) { // statement2 } ……... else { // statement(s) }
Example
let a=10,b=4; if(a<b) console.log("a is less than b."); else if(a>b) console.log("a is greater than b."); else console.log("a is equal to b."); // Output //a is greater than b.
if-else with relational operators in JavaScript
We use relational operators with if and if-else statements for testing conditions and making a decision.
Relational operators in JavaScript are:
- > greater than
- >= greater than or equal to
- < less than
- <= less than or equal to
- == equal to
- != not equal to
- === strict equality
- !== strict inequality
let a=9,b=9; if(a!=b) console.log("a and b are not equal."); else console.log("a and b are equal."); //a and b are equal.
If we use an assignment operator (=) instead of a comparison operator (==) in the condition then it will always return true because it performs assignment, not comparison.
if(a=12) console.log("Hii"); else console.log("Hello"); //Hii
So, I hope you have now gained all the information about conditional statements in JavaScript. In the next module, I will discuss the switch statement. Until then, stay connected with us for more tutorials like these. Keep Coding!