Js Switch Case

JavaScript Switch Case

In JavaScript, a switch statement allows you to select one of many code blocks to be executed based on a specific value. It is typically used when you need to compare a single expression (like a variable) against several possible values.

Basic Structure:

switch (expression) {
    case value1:
        // code to be executed;
        break;
    case value2:
        // code to be executed;
        break;
    // more cases…
    default:
        // code to be executed if no cases match;
        break;
}

Scenario: Pricing System Using JavaScript switch

In this scenario, we simulate a pricing system using a switch statement. The program will take the name of a car (Mustang, Ford, or another) and output the corresponding price. If no car matches, it will return “Invalid car name.”

Scenario 1: When car = "Ford"

js-switch-case

let car = “Ford”;

switch (car) {
    case “Mustang”:
        console.log(“Price is 1cr”);
        break;
    case “Ford”:
       console.log(“Price is 50 Lakhs”);
        break;
    default:
        console.log(“Invalid car”);
        break;
}

Output:
Code Explanation:

  Variable Declaration (let car = “Ford”;):

       •  In JavaScript, we declare the variable car using let, which holds the string value “Ford”.
       •  let is used for variable declaration (it allows reassignment and has block scope)
  Switch Statement:

       •  The switch statement is used to compare the value of the variable car to different case labels.
       •  The expression inside the switch is car, and the switch checks which case matches the value of car
  case “Mustang”:

       •  The first case checks if a car is equal to the string “Mustang”.
       •  Since the car is “Ford”, this case is skipped.
   case “Ford”:

       •  The second case checks if the car is equal to the string “Ford”.
       •  This matches because the car is indeed “Ford”.
       •  The code inside this case block is executed, so it prints “Price is 50Lakhs” to the console.

Scenario 2: When car = "Mustang"

let car = “Mustang”;

switch (car) {
    case “Mustang”:
        console.log(“Price is 1cr”);
        break;
    case “Ford”:
        console.log(“Price is 50 Lakhs”);
        break;
    default:
        console.log(“Invalid car”);
        break;
}

Output:
Scenario-2-When-car-Mustang-output
Code Explanation:

   Variable Declaration (let car = “Mustang”;):
       •  Here, we declare the variable car and set it to the string “Mustang”.
       •  let is used for variable declaration (it allows reassignment and has block scope)

   Switch Statement:
       • The switch expression is a car, and the statement evaluates which case matches the value of the car.
       • Since the car is “Mustang”, the switch will start checking each case

   case “Mustang”:case “Mustang”:
       • The first case checks if car is equal to “Mustang”.
       • Since car is indeed “Mustang”, this case block will execute , so it prints “Price is 1cr” to the console.

Scenario 3: When car = "Ferrari"

Scenario-3-When-car-Ferrari

let car = “Ferrari”;

switch (car) {
    case “Mustang”:
        console.log(“Price is 1cr”);
        break;
    case “Ford”:
        console.log(“Price is 50 Lakhs”);
        break;
    default:
        console.log(“Invalid car”);
        break;
}

Output:
Scenario-3-When-car-Ferrari-output
Code Explanation:

   Variable Declaration (let car = “Ferrari”;):
     •  Here, we declare the variable car and set it to the string ” Ferrari “.
     •  let is used for variable declaration (it allows reassignment and has block scope)

   Switch Statement:
     •  The switch expression is a car, and the statement evaluates which case matches the value of the car.
     •  Since the car is ” Ferrari “, the switch will start checking each case

   Default Case:
     •  If none of the case labels match the value of the car, the default case is executed.
     •  Since the car is ” Ferrari “, which doesn’t match either “Mustang” or “Ford”, the default case is triggered.
     • The message “Invalid car” will be printed on the console.

Course Video in Hindi

YouTube Reference:

Practice Scenarios

1. Identify Month Name:

Objective: Given a month number (1 to 12), print the corresponding month name using a switch statement.
Input: month = 5

Output:

2.Simple Calculator (Addition, Subtraction, Multiplication)

Objective: Perform basic arithmetic operations based on user input using a switch-case.
Input: operator = “+”, a = 5, b = 3

Output:
Practice-Scenarios-2

3.Determine Day Type

Objective: Given a day of the week number (1-7), check if it’s a weekend or weekday.
Input: day = 6

Output:
Practice-Scenarios-3

4.Check Grade

Objective: Based on a grade letter (A, B, C, D, F), print the description of the grade.
Input: grade = “B”

Output:
Practice-Scenarios-4

Input: grade = “A”

Output:

Input: grade = “C”

Output:

5.Calculate Shipping Cost:

Objective: Based on the country name, calculate the shipping cost
Input: country = “Canada”

Output:
Practice-Scenarios-5

6.Day of the Week:

Objective: Use a switch statement to display the corresponding day of the week based on a number (1 for Monday, 2 for Tuesday, etc.).
Input: day = 3;

Output:
Practice-Scenarios-6

7.Traffic Light:

Objective: Display the action to take based on the traffic light color.
Input: color = “red”;

Output:

8.Vowel Check:

Objective: Check if a character is a vowel or consonant.
Input: char = “e”;

Output:
Practice-Scenarios-8

Input: char = “f”;

9.Age Group:

Objective: Determine the age group based on age.
Input: age = 25;

Output:

Input: age = 7;

Output:

Input: age = 70;

Output:
Practice-Scenarios-9-3

10.Grade Evaluation

Objective: Use a switch statement to display a grade based on a score.
Input: score = 85;

Output:
Practice-Scenarios-10

Input: score = 92;

Output:
Practice-Scenarios-10-2

Input: score = 65;

Output:
Practice-Scenarios-10-3