Skip to main content

Conditions

A basic condition in javascript is,

if (condition) {
//do something
}

Let's take an example

const word = "Hello";

if (word.length < 10) {
console.log("Pass");
}

The above code will console "Pass".

else

You can also add else block for the cases,

const word = "Hello world";

if (word.length < 10) {
console.log("Pass");
} else {
console.log("Failed");
}

The above code will console "Failed".

else if

You can chain with several conditions using else if.

For example,

const integer = 0;

if (integer > 0) {
console.log("Integer is negative");
} else if (integer === 0) {
console.log("Integer is zero");
} else {
console.log("Integer is positive");
}

The above code wil console "Integer is zero".