Switch Scope

In this lesson, you will see two ways to use the switch statement and how those ways can affect the scope of variable declaration.

We'll cover the following...

A switch statement is similar to an if statement. The condition set between the two parentheses must be met to reach one of the cases. If not, the default case will be entered.

Press + to interact
function switchFunction(a: number): void {
switch (a) {
case 1:
let variableInCase1 = "test";
console.log(variableInCase1);
break;
case 2:
let variableInCase2 = "test2";
console.log(variableInCase2);
break;
default:
console.log("Default");
}
}
switchFunction(1);
switchFunction(2);
switchFunction(3);

Not adding the keyword break ...