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”;

Looping Statements

  1. This category of control statements allow your program to perform iterations. 
  2. Iteration is nothing but executing same code block multiple times.
    1. Printing numbers from 1 to 100
    2. Reading elements from an array.
  3. Following are the mainly used looping statements in JavaScript.
    1. for loop
    2. while loop
    3. do-while loop
    4. for-of loop
    5. for-in loop

for loop

  1. The for loop is one of the most commonly used loops.
  2. It is generally used when you know or can clearly define how the iteration should start, continue, and progress.
  3. for (initialization; condition; update) {
        // statements
    }
  4. for (let i = 1; i <= 5; i++) {

    console.log(i);

    }

While loop

  1. The while loop is called as condition-controlled loop
  2. The main idea behind this loop is keep executing this block while condition is true.
  3. while (condition) {

    // statements

    }
  4. let count = 1;

    while (count <= 5) {
    console.log(count);
    count++;
    }

do-while loop

  1. do-while loop is called exit-controlled loop because conditions will be checked after the loop body executes.
  2. do {

    // statements

    } while (condition);
  3. let number = 10;

    do {
    console.log(number);
    } while (number < 5);

for-of loop

  1. for…of was introduced as part of modern JavaScript’s iteration features.
  2. It is designed to iterate over the values of an iterable.
  3. An iterable is an object whose elements can be accessed sequentially.
  4. Common examples include:
    1. Arrays
    2. Strings
    3. Maps
    4. Sets
  5. const fruits = ["Apple", "Mango", "Banana"];

    for (const fruit of fruits) {
    console.log(fruit);
    }

for-in loop

  1. for…in is primarily designed to iterate over the property names (keys) of an object.
  2. const student = {

    name: "Rahul",

    age: 22,

    course: "React"

    };

    for (const key in student) {
    console.log(key);
    }
  3. This will produce below output
  4. name

    age

    course
Share.
Leave A Reply

Exit mobile version