Skip to content
the poor coder

switch case block scope

Poor Man
Published
Reading time
3 min read
On this page

Switch Case Block Scope#

Switch case is a control statement in JavaScript that allows you to perform different actions based on different conditions. The switch case statement evaluates an expression and matches the value of the expression to a case label. If a match is found, then the corresponding block of code is executed.

Block scope refers to the section of code where variables are accessible. In JavaScript, variables declared with the let or const keyword have block scope. This means that they are only accessible within the block of code in which they are defined.

How to Use Switch Case Block Scope#

The basic syntax for a switch case statement is as follows:

Text
switch(expression) {
   case value1:
      // code block
      break;
   case value2:
      // code block
      break;
   default:
      // code block
}

Let's say we want to write a program that takes an integer input from the user and outputs a message based on the value of the integer. We can use a switch case statement to accomplish this:

Text
// Get user input
let userInput = parseInt(prompt("Enter an integer:"));

// Evaluate user input
switch(userInput) {
   case 1:
      console.log("You entered 1.");
      let variableOne = "This variable is only accessible within this block.";
      break;
   case 2:
      console.log("You entered 2.");
      break;
   default:
      console.log("You did not enter 1 or 2.");
}

console.log(variableOne); // This will throw an error because variableOne is not accessible outside of its block.

In the above example, we declare a variable called variableOne within the block of code for the first case label. This variable has block scope and is only accessible within that block of code. If we try to access it outside of that block, we will get an error.

Multiple Ways to Use Switch Case Block Scope#

There are different ways to use switch case block scope. One way is to declare variables within each block of code:

Text
switch(userInput) {
   case 1:
      let messageOne = "You entered 1.";
      console.log(messageOne);
      break;
   case 2:
      let messageTwo = "You entered 2.";
      console.log(messageTwo);
      break;
   default:
      let messageDefault = "You did not enter 1 or 2.";
      console.log(messageDefault);
}

In this example, we declare a separate variable for each case label. Each variable has block scope and is only accessible within its block of code.

Another way to use switch case block scope is to declare variables before the switch statement and update their values within each block of code:

Text
// Declare variables before switch statement
let message = "";
let variableTwo = "";

switch(userInput) {
   case 1:
      message = "You entered 1.";
      variableTwo = "This variable is accessible outside of its block.";
      break;
   case 2:
      message = "You entered 2.";
      break;
   default:
      message = "You did not enter 1 or 2.";
}

console.log(message);
console.log(variableTwo); // This will output the value of variableTwo because it was declared before the switch statement.

In this example, we declare two variables before the switch statement. These variables are accessible throughout the entire code block. We update the value of the message variable within each block of code. We also update the value of the variableTwo variable within the block of code for the first case label. This variable is now accessible outside of its block because it was declared before the switch statement.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC