What are Control Statements?

  1. Normally JavaScript executes the program from top to bottom. Control statements allow you to change this normal flow.
  2. Control Statements are classified into three types
    1. Decision Making Statements
    2. Looping Statements
    3. Jump Statements

Decision Making Statements

  1. This category of conditional statements allow your program to make decisions.
  2. E.g. If user logged in show dashboard else show login page.

  3. Following are the decision making statements
    1. If…else 
    2. Switch
    3. Ternary

If statement

  1. This is the simplest conditional statement.
  2. Syntax
  3. if (condition) {

    // code

    }
  4. If the condition gets false then block doesn’t executes.
  5. A condition normally produces a Boolean value.
  6. let age = 15;

    if (age >= 18) {
    console.log("You are eligible to vote");
    }

If…else statement

  1. When there are two possible paths then you can use If along with else statement.
  2. if (condition) {

    // true

    } else {

    // false

    }
  3. let age = 16;

    if (age >= 18) {
    console.log("Adult");
    } else {
    console.log("Minor");
    }

 

If…else if…else

  1. You can use this when there are multiple conditions.
  2. JavaScript checks conditions from top to bottom as soon as one condition is true then if block executes and remaining else if conditions skipped.
  3. let marks = 85;

    if (marks >= 90) {
    console.log("Grade A+");
    } else if (marks >= 75) {
    console.log("Grade A");
    } else if (marks >= 60) {
    console.log("Grade B");
    } else if (marks >= 40) {
    console.log("Grade C");
    } else {
    console.log("Fail");
    }

 

Multiple Conditions

  1. JavaScript provides logical operators so that we can apply multiple conditions.
  2. AND – && : Both conditions must be true.
  3. let age = 25;
    let hasLicense = true;

    if (age >= 18 && hasLicense) {
    console.log("Allowed to drive");
    }
  4. OR -|| : At lease one condition must be true.
  5. let isAdmin = false;
    let isManager = true;

    if (isAdmin || isManager) {
    console.log("Access granted");
    }
  6. NOT – ! : Reverses a boolean value
  7. let isLoggedIn = false;

    if (!isLoggedIn) {
    console.log("Please login");
    }

 

Switch Statement

  1. Switch statement is useful when you want to compare one value against multiple possible values.
  2. switch (expression) {

    case value1:
    // code
    break;

    case value2:
    // code
    break;

    default:
    // code
    }
  3. let day = 2;

    switch (day) {

    case 1:
    console.log("Monday");
    break;

    case 2:
    console.log("Tuesday");
    break;

    case 3:
    console.log("Wednesday");
    break;

    default:
    console.log("Invalid day");
    }
  4. O/P: Tuesday

 

Ternary Operator

  1. It is a short form of if…else statement.
  2. Syntax: condition ? valueIfTrue : valueIfFalse
  3. let isLoggedIn = true;

    let message = isLoggedIn
    ? "Welcome"
    : "Please login";

 

Share.
Leave A Reply

Exit mobile version