Loading...
Loading...
00:00:00

Conditional Statement

In JavaScript, conditional statements are used to execute different blocks of code based on a specific condition.

If Statement

In the if()conditional statement we can write the expression and the expression is evaluated in true or false, the given condition is true then execute the if block code. Here is the syntax

if (condition) 
{
    //code execute when condition true
}

Example

if block only executes when condition becomes true. Here is the example to check whether x is equal to 10 or not.

let x = 10;
if (x == 10) 
{
    document.write("x is equal to 10");
}

If Else

if else conditional statement has two block of code. if() statement with else()part when given condition is become true then execute the if block of code, when given condition is become false then execute the else block of code. syntax is here

if (condition) 
{
    //execute when condition true
} 
else 
{
    //execute when condition false
}

Example

Here is an example to demonstrating how to implements if else conditional statement. In this code we are checking the given value of x is greater than 10 or less than 10.

let x = 8;
if (x > 10) 
{
    document.write("x is greater than 10");
} 
else 
{
    document.write("x is less than 10");
}

Else If

else if statement is used to apply more condition with if statement. we can write multiple if condition with else if statement. here is the example to check a number is greater than 10 or less than 10 or equal to 10.

let x = 15;
if(x > 10)   //first condition
{   
    // block of code executes when if condition is true 
    document.write("x is greater than 10");  
}
else if(x < 10)   // second condition
{
    // block of code executes when else if condition is true 
    document.write("x is less than 10");
}
else
{
    // block of code executes when all condition is false 
    document.write("x is equal to 10");
}

Switch Case Syntax

switch case conditional statement is used to select block of code to execute when condition is match to any case. It performs different action on different condition. Here is the syntax

switch (expression) {
    case value:
        // code 
        break;

    default:
        break;
}

Switch Case And Default

in switch parenthesis we can write expression, condition, string, or number. Condition will be evalute and jump to the case when match with anyone. break keyword must be used when case is end. if condition does not match any case it will goes to default block. here is the example to show the week-day by numbers.

 let x = 2;
switch (x) {    //condition or expression 
    case 1:
        document.write("Monday");
        break;
    case 2:
        document.write("Tuesday");
        break;
    case 3:
        document.write("Wednesday");
        break;
    case 4:
        document.write("Thursday");
        break;
    case 5:
        document.write("Friday");
        break;
    case 6:
        document.write("Saturday");
        break;
    case 7:
        document.write("Sunday");
        break;

    default:    // execute when case does not match 
        document.write("Invalid week day!");
        break;
}