JavaScript Switch Statement
JavaScript switch statement is a control statement that allows you
to execute different code blocks based on the value of a variable or an
expression. It provides a way to write more concise and readable code when you
have multiple conditions that need to be evaluated.
Here's the syntax of a switch statement:
Example:
Write a JavaScript program to input daycode and display the day name.
In above JavaScript Switch Statement program, the prompt() method is used to get input from the
user. The parseInt() function is used to convert the input to a number.
Then, a switch statement is used to match the daycode to the
corresponding dayname. Each case has a statement that assigns the dayname
variable and logs a message to the console with an explanation of that day.
If the daycode entered by the user is not a valid number between 1 and
7, the default case is executed, which logs an error message to the console.
Output:
No comments:
Post a Comment