Conditional Statement in JavaScript with Examples


In this JavaScript tutorial, we are going to learn about Conditional Statements in JavaScript.


What are Conditional Statements?

  • Conditional Statements are used to control the flow of execution of the program.

  • These are similar to Decision-Making in real-life.

  • Conditional Statement returns true or false and the code is executed on the given return value.


Conditional Statements are:

  • if
  • if-else
  • if-else ladder
  • switch


JavaScript Conditional Statements with Examples


1. if Statement

  • In the if statement, if the given condition is true then it will execute the code.

Syntax:

if (condition) {
    //code to be executed
}

Example:

let num = 20;

if (num > 18) {
    console.log('You can drive the car');
}

//Output: You can drive the car

In the given example the value of num is greater than 18 so the code inside the if statement will be executed.


2. if-else Statement

  • In this, if the given condition is true then it will execute the if block otherwise the else block will be executed.

Syntax:

if (condition) {
    //code to be executed
}
else {
    //code to be executed
}

Example:

let age = 10;

if (age > 18) {
    console.log('You can drive the car');
}
else {
    console.log('You cannot drive the car');
}

//Output: You cannot drive the car

In the given example the value of age is less than 18 so the code inside the if statement will not executed so the else block will be executed.


3. if-else ladder Statement

  • In this statement, there are multiple if blocks and also known as else-if ladder.

Syntax:

if (condition) {
    //code to be executed
}
else if (condition) {
    //code to be executed
}
else {
    //code to be executed
}

Example:

let age = 5;
if (age > 18) {
    console.log('You can drive the car');
}
else if (age > 22) {
    console.log('You can drive bus');
}
else {
    console.log('Enter a valid age');
}

//Output: Enter a valid age

In the given example the value of age is less than 18 so the first if-block will return false. Then it moves to the next block and in the next else-if block the value of age is less than 22 so it will also returns false and then the else block will be executed.


4. switch Statement

  • Switch takes an expression as input and matches it with different cases.

  • If the value of expression match with any case then it will execute the code of the given case.
  • And if the value doesn't match expression then it will automatically execute the default.

Syntax:

switch (expression) {
    case 1:
    //code;

    case 2:
    //code;

    default:
}

Example:

let age = 20;

switch (age) {
    case 10:
        console.log('10');
        break;

    case 20:
        console.log('20');
        break;

    default:
        console.log('default');
}

//Output: 20

In the given example the value of age is 20 and it matches with case 20:

So, the code inside case 20: will be executed.


Note: We will learn about the break and continue in upcoming tutorials.


Importance of Conditional Statements 

1. Decision Making: Conditional statements are used for decision-making within code. Because conditional statements allow you to evaluate conditions and execute different blocks of code based on conditions whether it is true or false.

2. Control Flow: These statements provide control over the flow of execution in the code. By creating certain conditions you can determine which section of code should be executed and in which order.

3. Error Handling: Conditional statements help in error handling and exceptions gracefully.

4. Code Efficiency: Conditional statements allow you to write efficient code by eliminating the need for unnecessary computations or actions.

5. Program Logic and Control: These statements are fundamental to implementing program logic and control structures. Conditional Statements allow you to express complex logical operations, like combining multiple conditions with logical operators (AND, OR), nesting conditions, or creating branching paths in code.

6. User Interaction: These are the essential statements for building interactive web applications. Conditional Statement enables you to respond to user's input or events based on specific conditions

7. Customization and Personalization: Conditional statements enable you to create personalized experiences for users.


Conditional Statement in JavaScript with Example PDF: Download Now


Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad

Ads Section