Copyright

Conditional Statements in JavaScript

Instructor: Vignesh Sivabalan
This lesson gives an insight into the different conditional statements in JavaScript: if, else, else if and switch statements. Each JavaScript conditional statement is made clear to the learners with definition, syntax and examples.

Conditional Statements in JavaScript

The conditional statements included in the JavaScript code assist with decision making, based on certain conditions. The condition specified in the conditional statement can either be true or false. The conditional statements execute the associated piece of code only if the condition is true. We have four types of conditional statements in JavaScript:

  1. An if statement executes a specified code segment if the given condition is ''true.''
  2. An else statement executes the code associated with the else if the given if condition is ''false.''
  3. An else if statement specifies a new condition for testing when the first if condition is ''false.''
  4. A switch-case statement specifies multiple alternative code blocks for execution and executes these cases based on certain conditions.

The if Conditional Statement

The conditional if statement specifies a JavaScript code for execution only if the given condition is ''true.''

Syntax

if (condition)

{

//code to get executed when the above specified condition is true

}

Example:

if (age>=18)
{
eligibility= 'Votable age';
}

Explanation:

The above if statement checks if the variable age is greater than or equal to 18. If it is greater than or equal to 18, it assigns ''Votable age'' to the variable eligibility. For age values less than 18, the condition fails and the code associated with the if statement will not be executed.

The else Conditional Statement

When the condition inside the if statement is false, the code associated with the else statement gets executed.

Syntax:

if (condition)

{

//code to get executed when the condition is true

}

else

{

//code to get executed when the 'if' condition fails

}

Example:

if (age>=18)
{
eligibility= 'Votable age';
}
else
{
eligibility='Not votable age';
}

Explanation:

If the value of the age variable is >=18, the code associated with the if condition is executed. On the other hand, if the age variable's value is <18, the code associated with the else statement is executed and ''Non votable age'' is assigned to the variable eligibility.

The else-if Conditional Statement

When the first condition fails, the else-if allows the program to specify some new condition(s).

Syntax:

if (condition_one)

{

//code to execute if condition_one is true

}

else

{

//code to execute if the condition_one is false

}

else if (condition_two)

{

//code to execute if the condition_one fails but the condition_two is true

}

Example:

if (marks > 90 && marks <= 100)
{
grade = 'A+';
}
if (marks > 80 && marks <= 90)
{
grade = 'A';
}
else if (marks > 70 && marks <= 80)
{
grade= 'B';
}
else if (marks > 60 && marks <= 70)
{
grade = 'C';
}
else if (marks > 50 && marks <= 60)
{
grade='D';
}
else
{
grade='fail';
}

Explanation:

In the above example, multiple else if statements are used.

  • The first condition checks if the marks are between 100-90 and assigns A+ to the grade variable if the condition is true.
  • If the first condition fails, the second condition checks if the marks are between 80 to 90 and assigns A to grade if the condition is true.
  • If the second condition fails, the third condition checks if the marks are between 80 to 70 and assigns B to grade if the condition is true.
  • If the third condition fails, the fourth condition checks if the marks are between 70 and 60 and assigns C to grade if the condition is true.
  • If the fourth condition fails, the fifth condition checks if the marks are between 60 and 50 and assigns D to grade if the condition is true.
  • If all the above conditions fail, the else statement is executed and the grade is assigned the value ''fail''.

The Switch Case Statement

The switch case statement has an expression which is compared with values of each case statement and if a match is found, the associated code is executed.

Syntax:

switch(expression)

{

case a:

//block of code

break;

case b:

//block of code

break;

.

.

.

default:

//block of code

}

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support