Lesson 10 Conditionals in JavaScript

In this part, we will cover one of the most important concepts of every programming languages which is conditionals.
In this lesson, we will also learn about the conditional statement like if, else and else if, these statements help us to add logic to our programming.

So conditional is something like that you are trying to log in to your Facebook account and Facebook want your identity like email address and password to allow you to login to Facebook, so you have to type your email address and password in the Facebook Login form and if that password match to the Facebook database, you will be redirected to another page and if they don’t match you usually get an error message.

So we often use JavaScript conditional statements to do different actions which are based on different conditions.

Conditional Statements

So there are three keywords that I need you to remember throughout this lesson which is below:
1. If
2. Else
3. Else if

IF Statement

So the first thing we are going to see is the if statement if keyword takes a single expression inside of parentheses which it will evaluate to be true or false and if it evaluates to be true whatever code we provide inside the curly braces will be executed.

Syntax:

Here is the syntax for if statement in javascript

if (Condition1) 
{ 
This line of code will be executed if the condition1 is true
}

Example:

If your age is under 18 you cannot drive the car, here is an example of JavaScript code below:
var age = prompt("whats your age?"); 
if (age<18) { 
alert("sorry, you are too young to drive car"); 
}

Output

When we write above code in our Google Chrome console and hit enter, then my console executed my JavaScript code and asked for my age.
img1

And when we typed 10 and press ok button then it shows me alert dialog box and saying “sorry you are too young to drive car” message

img2

ELSE Statement

Else statement is directly linked to if statement, Else statement will be executed if the if- statement turns out to be false. Here is a syntax for Else statement below,

Syntax:

if (Condition1) {
This line of code will be executed if the condition1 is true
} 
else {
This line of code will be executed if the condition1 is false

}

Example:

If your age is under 18, you cannot drive the car but you can drive the car if you are older than 18.
Here is JavaScript code for this statement:

var age = prompt("whats your age?"); 
if (age<18) { 
alert("sorry, you are too young to drive car"); 
}
else {
alert("You can drive car");
}

Output

Here you can see when we executed JavaScript code, it asked me my age and this time I am putting a value which is greater than 18.
img3

And when we press ok, an alert dialog popped up and showing a message that “You can drive car”

img4

Else if Statement

So the last statement we are going to see here is Else if statement, so this statement works exact same way as if statement, this is basically a series of if statement.
So if we pass an expression in parenthesis that will evaluate to be true or false, if it turns out to be true then the code inside the curly braces will be executed.

Syntax:

So the synatx for else if statement in javascript is there below:

if (Condition1) {
This line of code will be executed if condition1 is true
} else if (Condition2) {
This line of code will be executed if the condition1 is false and condition2 is true
} else {
This line of code will be executed if the condition1 is false and condition2 is false
}

In this lesson, we have learned how to use the conditional statement in JavaScript and we have also given a detailed overview of IF, ELSE IF, and ELSE statement with examples.

>Continue to Lesson 11

Javascript Lessons for beginners

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?