JavaScript if else

Profile picture for user arilio666

Same as the if statement and nothing more except another condition tethered with it called else. else is nothing but another condition where its body is invaded when the if body fails to meet the logic.

Syntax:

if(){

}else{

}

The syntax is similar to if with an extra else block below with it.

Example:


let salary = 50000;
if(salary <= 30000){
console.log('Salary Credited')
}else {
   
    console.log('Out of block')

}

Output: Out of block

This is the same example we used for if article. Here the salary logic does not satisfy the if block. So it goes below to else and executes that part alone.

Verdict:

The if..else paired up can be a great conditional statement for comparison purposes and other logical purpose too.