Switch case :
The switch statement provides a concise way to handle multiple cases and make decisions based on
different values of an expression. It's particularly useful when you have a limited set of
possible values and want to avoid lengthy if...else chains.
The switch statement in JavaScript provides a way to perform different actions based on
different
conditions. It allows you to compare an expression against multiple values and execute a block of
code based on a matching value. Here's the basic syntax of the switch statement:
switch (expression) { case value1: // Code block executed when expression matches value1 break; case value2: // Code block executed when expression matches value2 break; // Additional cases... default: // Code block executed when no case matches the expression }
Here's how the switch statement works:
Once the expression has been evaluated, its value is compared with those of the case clauses.
Each case clause represents a possible value that the expression can match. If the value in a
case clause matches the value of the expression, the corresponding code block is executed.
If a match is discovered, the case-specific code block is run up until a break statement is
reached. The switch block is terminated and the execution of the following code blocks is stopped
using the break statement. Until a break or the end of the switch block is reached, execution
will move on to the following case in the absence of a break statement.
It's important to use the break statement appropriately to control the flow of execution
within the switch block.
Here's an example to illustrate the usage of switch statement:
let day = 3; let dayName; switch (day) { case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; case 4: dayName = "Thursday"; break; case 5: dayName = "Friday"; break; default: dayName = "Weekend"; } console.log(dayName); // Output: "Wednesday"
In this example, the value of the day variable is compared against different cases. Since day is
3, the case 3 matches, and the corresponding code block sets the value of dayName to "Wednesday".
The break statement ensures that execution exits the switch block.
It's important to note that the switch statement compares values using the strict equality operator (===),
so the values must match both in value and type for a case to be considered a match.
Let's create a program that determines the animal based on a given animal code, using a JavaScript
switch case and document.write to display the result. Since we are using document.write, it means
we can use this below code along with an html file only and give the path of this js file
in html file script tag.
Here's the code:
function getAnimalName(animalCode) { let animalName; switch (animalCode) { case 1: animalName = "Lion"; break; case 2: animalName = "Elephant"; break; case 3: animalName = "Tiger"; break; case 4: animalName = "Giraffe"; break; case 5: animalName = "Zebra"; break; default: animalName = "Unknown animal"; } return animalName; } // Using document.write to display the result const animalCode = 3; const animalName = getAnimalName(animalCode); document.write("The animal is: " + animalName);
In this example, the getAnimalName function takes an animalCode as input and returns the
corresponding animal name as a string. We use a switch case statement to match the animalCode
with the appropriate animal name. If the animalCode doesn't match any of the cases, the default
case is triggered, and the function returns "Unknown animal".
To display the result, we use document.write to write the text "The animal is: "
concatenated with the animalName returned by the function. This will display the result on the
webpage where the JavaScript code is executed.
Feel free to run the code and change the animalCode variable to see different results displayed
using document.write. Note that using document.write directly in production code is generally
discouraged as it can overwrite the entire document content if used after the page has finished
loading. It's better to manipulate the DOM or use other methods for displaying content.