JavaScript has a feature to execute codes on a conditional basis or whether the situation is true or false.
There are different forms of conditional rendering:
- if statement
- if-else statement
- if, else if statement
- ternary operator
IF statement:
The main idea of the "if" statement is to perform a specific task under a condition or else leave that task if the condition is not fulfilled.
Syntax:
if(condition){
//code to be executed
}
Example:
if( 4%2 === 0){
console.log(‘This is an even number’);
};
In the above example, we can see that if the condition gets true then the JavaScript will render the code inside the if statement and will display the string ‘This is an even number’.
NOTE: The code out of if statement will still render if it is inside a function as a whole. This means that for example if we create a function with an if statement inside it and also another code out of the if statement but inside the function.
function evenNumber (){
if( 4%2 === 0){
console.log(‘This is an even number’);
};
Console.log(‘I will still get displayed’);
};
In the above code when the function evenNumber(); will be called it will display two strings ‘This is an even number’ and also ‘I will still get displayed’.
IF and ELSE Statement
While using the "if" and "else" statement it will determine to execute one of the two sets of code on the basis of conditions or true/false aspects.
Syntax:
if(condition 1){
//code to be executed if condition 1 is true
}else{
//code to be executed if condition 1 is not true
};
Example:
if(a > 100 ){
console.log(‘a is greater than 100’);
}else{
console.log(‘a is not greater than 100’);
};
For the above code if the given variable has a value greater than 100 it will execute the code under the if statement or else will perform the code under the else statement.
IF and ELSE IF Statement
If and if-else statements are generally used when there are many conditions given by the user. To carry out this we generate many if-else statements followed by one if statement. Which in case if turns out to be false then we can move forward with the other conditions stated inside the number of if-else statements.
Syntax:
if(condtion1){
//execute this code if condition 1 is fulfilled
}else if (condtion 2){
//execute this code of condition 2 is fulfilled
}else if
.
.
.
else{
//execute this code if none of the above conditions are fulfilled
};
Example:
function calculatorBox (){
a = 2;
b = 4;
if( a >b){
c = a/b;
else if( a === b){
c = b;
}else{
c = b/a;
};
};
In the above code, we can see that when the calculatorBox(); functions will be called the JavaScript go through each condition of if and else if statement. When it will find the condition to be true then it will execute the code inside the respective condition that is c = b/a here.
Ternary Operator
It is shorthand of using if, else, and else if statement. For making the usage of conditional rendering easier with less code we use ternary operators.
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy. This operator is frequently used as a shortcut for the if statement. – MDN Web-Docs
Syntax:
condition ? if-true-expression : if-false-expression ;
Example:
const legalAge = (age>18) ? “eligible for license” : “underage for license” ;
In the above code there is function legalAge(); which is getting value under conditional rendering that is if the age is greater than 18 then the expression/code before: will execute and in case if the condition is not fulfilled then the later part will be executed.
Miscellaneous Part for Ternary Operator
We can use multiple ternary operators. This means that there can be a sequence of conditions and each one can be implemented using ternary operators.
For example:
let message = (age < 18) ?’Not an legal age for driving in India!' :
(age < 25) ? 'You can drive but can not consume alcohol in India!’ :
(age < 30) ? 'You are good to go!' :
'Always obey the law';
console.log( message );
In the above code, there are just some ordinary sequences of tests to check the condition and on the basis of that, it will display the message. If age is 19 then it will display You can drive but can not consume alcohol in India!. Just in case if the age does not match any condition (for e.g., age = 31) it will simply display Always obey the law.


