• in
InterCourses
CoursesBlogs
0
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking
○Declaring Variables○let and const in Practice○Operators○Type Conversion
○Conditionals●Switch Statement○For Loops○While Loop○Break and Continue
○Defining Functions○Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions
○Arrays○Array Mutation Methods○Array Search and Slice○Array Iteration Methods○Multidimensional Arrays○Destructuring○Array Reverse and Sort○Array Fill○Array Find and Includes○Array Flat and FlatMap○Array Reduce, Some, and Every
○Objects○Object Methods and Destructuring○JSON○Math and Date○Date Basics
○02 Template Literals○String Methods○Regular Expressions
○Higher-Order Functions○Callbacks○map, filter, and reduce○Recursion○Project - Data Transform Pipeline
○OOP Introduction○Classes○Inheritance○Encapsulation
○Asynchronous JavaScript○Promises and Async Await○Async API Simulation

Switch Statement

The switch statement is an alternative to long if/else if chains when you are matching a single value against several discrete options.

Syntax

javascript
switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code when no case matched
}

Important: Always add break at the end of each case. Without it, execution "falls through" to the next case.

Fall-through (sometimes intentional)

javascript
let day = "Saturday";
switch (day) {
    case "Saturday":
    case "Sunday":
        console.log("Weekend!");
        break;
    default:
        console.log("Weekday");
}

Your Task

  1. Write a function getDayType(day) that accepts a day name (e.g. "Monday") and returns:
    • "Weekend" for "Saturday" or "Sunday"
    • "Weekday" for any other day name
    • "Unknown" if the input is not a valid day name
  2. Call getDayType("Saturday") and display the result in #output as "Saturday: Weekend".
Hint 1
1 / 3
HINT 1

Each case matches the value after switch (expression).

Loading editor…
READY
intercourses
javascript