JavaScript Conditions

The if statement

In this lession we will learn about conditional flow of the code. JavaScript support various conditional statement like if, if else and switch conditional statements.

The if statement will evaluate the given condition and based on the result of the condition it will decide which code block to execute.

For example if we want to check users age and based on the age we want to display the result then we use if condition as below

Example
var age = 2;
if(age > 2)
{
	console.log("You need to purchase a ticket");
}

If-Else statement

We can use if else condition if we want to execute either of the code blocks. For example if we want to tell user that if you want to subscribe for the services then you need to do payment else payment is not required. We can write code as below

Example
var subscribeForService = true;
if(subscribeForService == true)
{
	console.log("You need to make a payment for subscription");
}
else
{
	console.log("You can skip this step");
}

In if and if-else condition we can use ==, ===, !=, >, >=, < and<= operators to evaluate condition based on the requirements, I am not going to covert Example in this lession. I am leaving those excercises for you to practice.

If we want to check that two variables have same value then == operator is used. If we want to check that value is same and type of the value is also same then === operator will be used. Below example will show you how we can use == and === operator works.

Example
var age = "20";
if (age === 20)
{
    console.log("Type of the age variable is integer.");
}
else if (age == 20)
{
    console.log("The age variable type is non integer.");
}

We can also evaluate more than 1 condition in single if statement with the help of logical operator. If we want to check wether user has 18 year old and has voter id card then user can vote else user can't vote then we can check like this.

Example
var age = 18;
var hasVoterId = true;
if(age >= 18 && hasVoterId == true)
{
	console.log("you can vote");
}
else
{
	console.log("you can't vote");
}

In above code we have used && operator, we can also user || (OR) operator if we want to check wether anyone condition is true from given condition. We will see in more details about operators in operatior lession.

The switch statement

If we have more than 2-3 conditions then it's better to use switch statement. It is similar to the other languages like C or C++. In switch statement we need to define 1 case for each condition and then break statement at the end to break the execution from switch statement. If we forget to write break statement then it will also execute next case and continue untill it's encounter break statement. For example

Example
var department = "HR";
switch(department)
{
    case "Developer":
        console.log("You can access only employee module.");
        break;
    case "HR":
        console.log("You can access all the module except Management module");
        break;
    case "Finance":
        console.log("You can access only finance module");
        break;
	case "Management":
        console.log("You can access all the modules");
        break;
    default:
        console.log("Sorry you are not authorised to access anything");
        break;
}

If any of the case statement will not evaluate true then it will execute default case, however default case is an optional case so we can ommit it if it is not required.

 

← Previous

Next →