TypeScript switch Statement

TypeScript's switch statement acts like a musical conductor, directing your program's execution based on different "instruments" (values) it receives. Unlike if-else's linear choices, it allows for a multi-branched, efficient way to handle various cases.

Here's the basic syntax of a TypeScript switch statement:

switch (expression) { case value1: // code to be executed if expression matches value1 break; case value2: // code to be executed if expression matches value2 break; // additional cases as needed default: // code to be executed if none of the cases match }

switch statement with examples:

let day = "Monday"; switch (day) { case "Monday": console.log("Coffee time!"); break; case "Tuesday": case "Wednesday": case "Thursday": console.log("Time to work!"); break; case "Friday": console.log("TGIF! "); break; default: console.log("Weekend vibes "); }

This checks the value of the day variable. Each case clause matches a specific day, and its corresponding code block is executed. The default clause catches any unmatched values.

Using expressions and strict comparison

let score = 85; switch (true) { case score >= 90: console.log("Excellent!"); break; case score >= 80: console.log("Great job!"); break; case score >= 70: console.log("Good work!"); break; default: console.log("Keep practicing!"); }

This utilizes expressions inside case clauses and strict comparison. This allows for checking ranges or more complex conditions.

Combining switch with type narrowing

type Fruit = "apple" "banana" "grapefruit"; function categorizeFruit(fruit: Fruit): string { switch (fruit) { case "apple": return "A sweet and crunchy treat."; case "banana": return "A potassium-rich power source."; default: return "A delicious and nutritious option."; } } let myFruit = categorizeFruit("apple"); console.log(myFruit); // Output: "A sweet and crunchy treat."

This example showcases how switch can be used with TypeScript's type narrowing. Based on the fruit's type, a specific description is returned. This makes code more precise and eliminates unnecessary checks.

Using fallthrough with caution

let grade = "B"; switch (grade) { case "A": case "B": console.log("You passed!"); break; // Remember to break to avoid unintended fallthrough case "C": case "D": console.log("Work harder next time."); break; default: console.log("Please re-take the exam."); }

In this example, the B case intentionally doesn't have a break statement. This lets the execution "fall through" to the next case ("C"). Use this with caution, as accidental fallthrough can lead to unexpected behavior.

Early exit with return

function isVowel(character: string): boolean { switch (character.toLowerCase()) { case "a": case "e": case "i": case "o": case "u": return true; default: return false; } } let result = isVowel("o"); console.log(result); // Output: true

This demonstrates using switch in a function with an early exit via return. This makes the code more concise and efficient.

Conclusion

The switch statement is a control flow structure used for conditional branching based on the value of an expression. It allows developers to compare the expression against multiple cases and execute the code block associated with the first matching case, with an optional default case for unmatched values. The break statement is used to exit the switch statement after executing the relevant code block.