Looping Statements in JavaScript || Switch Statement

2 minute read
0

 Looping Statements In JS

A looping declaration in JavaScript is a programming construct that lets in you to execute a block of code time and again. Looping statements are beneficial while you need to perform a certain action a couple of times, or when you need to iterate over a collection of facts.

In JavaScript, there are numerous looping statements to be had, along with:



The for loop: This loop is used to execute a block of code a precise range of instances, with a counter variable that may be used to tune the variety of iterations.
Example:-

for (let i = 0; i < 5; i++) { console.log(i); } The while loop: This loop is used to execute a block of code so long as a distinctive circumstance is genuine. It is normally used when you don't know how typically the loop will need to run.
Example:-

let i = 0;

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

The do-while loop: This loop is similar to the whilst loop, however it'll usually execute the block of code at least as soon as, despite the fact that the circumstance is fake.
Example:-

let i = 0;

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

The for...In loop: This loop is used to iterate over the homes of an object.
Example:-

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};

for (let property in person) {
  console.log(`${property}: ${person[property]}`);
}

The for...Of loop: This loop is used to iterate over the values of an iterable object, along with an array.
Example:-

const myArray = ['apple', 'banana', 'orange'];

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

Switch Statement in JS

The switch statement is a type of conditional statement that allows you to compare a variable against multiple values and execute different code depending on which value the variable matches.

Example:-

const day = 'Monday';

switch (day) {
  case 'Monday':
    console.log('It is Monday!');
    break;
  case 'Tuesday':
    console.log('It is Tuesday!');
    break;
  case 'Wednesday':
    console.log('It is Wednesday!');
    break;
  default:
    console.log('It is some other day.');
    break;
}

In this example, we have a variable day that contains a string value of 'Monday'. We then use a switch statement to compare day against multiple cases.

Each case represents a possible value that day could be. If day matches one of the case values, the corresponding code block will be executed. In this case, since day is 'Monday', the code in the first case block will execute, logging "It is Monday!" to the console.

If day does not match any of the case values, the code in the default block will execute. In this example, the default block simply logs "It is some other day." to the console.

Note that each case block must end with a break statement. This tells JavaScript to exit the switch statement and continue executing the rest of the code outside of the switch. If you forget to include a break statement, JavaScript will continue executing the code in the next case block even if the case value does not match.

Post a Comment

0Comments
Post a Comment (0)