Using if-else Statement to coding in Javascript/JS

This JavaScript tutorial explains how to use the if-else assertion with syntax and examples.

Description

In JavaScript, the if-else announcement is used to execute code when a situation is TRUE, or execute special code if the situation evaluates to FALSE.

Syntax

There are exceptional syntaxes for the if-else statement.

Syntax (if)

The syntax for the if statement in JavaScript is:

if (condition) {
   // statements to execute when condition is TRUE
}

You use the if syntax, when you desire to execute statements only when the circumstance is TRUE. Although it is fine exercise to use { }, you can pass over them if there is solely one declaration to execute as in the following syntax:

if (condition)
   // only one statement to execute when condition is TRUE

Syntax (if-else)

The syntax for the if assertion observed by way of an else in JavaScript is:

if (condition) {
   // statements to execute when condition is TRUE

} else {
   // statements to execute when condition is FALSE

}

You use this syntax, when you want to execute one set of statements when the circumstance is TRUE or a special set of statements when the circumstance is FALSE.

Syntax (else if)

The syntax for the else if declaration in JavaScript is:

if (condition1) {
   // statements to execute when condition1 is TRUE

} else if (condition2) {
   // statements to execute when condition1 is FALSE and condition2 is TRUE

} else {
   // statements to execute when both condition1 and condition2 are FALSE

}

The else if is created by means of nesting multiple if statements. You use this syntax, when you choose to execute one set of statements when condition1 is TRUE, a exclusive set of statements when condition1 is FALSE and condition2 is TRUE, or a exceptional set of statements when all preceding stipulations (ie: condition1 and condition2) are FALSE.

Note

Once a condition is located to be TRUE, the if-else assertion will execute the corresponding code and now not consider the stipulations any further. If no circumstance is met, the else portion of the assertion will be executed. It is essential to be aware that the else if and else parts are optional.

Example

The following is instance the use of the if-else announcement in JavaScript:

// Set the TechOnTheNet technology to JavaScript
var totn_technology = 'JavaScript';

if (totn_techonology == 'SQL' ) {
    console.log('TechOnTheNet SQL');
    
} else if (totn_technology == 'JavaScript') {
    console.log('TechOnTheNet JavaScript');

} else {
    console.log('Other TechOnTheNet technologies');
}

In this if-else statement example, the code will execute one-of-a-kind statements depending on the cost of the totn_technology variable. Since the totn_technology variable has been set to the string ‘JavaScript’, the statements inside the else if area of the code will be executed.

In this example, the following will be output to the internet browser console log:

TechOnTheNet JavaScript