- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain common code blocks in JavaScript switch statement?
Following is the code to implement common code blocks in JavaScript switch statement −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>JavaScript Switch statement</h1> Enter day 1-7<input type="text" class="day" /><button class="Btn"> CHECK </button> <div style="color: green;" class="result"></div> <h3> Click on the above button to check which day it is </h3> <script> let dayVal = document.querySelector(".day"); let resEle = document.querySelector(".result"); document.querySelector(".Btn").addEventListener("click", () => { switch (parseInt(dayVal.value)) { case 1: resEle.innerHTML = "It's monday"; break; case 2: resEle.innerHTML = "It's tuesday"; break; case 3: resEle.innerHTML = "It's wednesday"; break; case 4: resEle.innerHTML = "It's thursday"; break; case 5: resEle.innerHTML = "It's friday"; break; case 6: resEle.innerHTML = "It's saturday"; break; case 7: resEle.innerHTML = "It's sunday"; break; default: resEle.innerHTML = "Enter a value between 1 - 7"; break; } }); </script> </body> </html>
Output
The above code will produce the following output −
On entering a number between 1-7 and clicking on CHECK −
- Related Articles
- Explain Strict Comparison in JavaScript switch statement?
- Explain switch statement in C language
- What is Switch...case statement in JavaScript?
- Can we have a return statement in a JavaScript switch statement?
- Explain the scope rules related to the statement blocks in C language
- Switch Statement in Java
- Java switch statement
- PHP switch Statement
- Switch case statement in C
- Explain "for...in" statement in JavaScript?
- Java switch statement example
- Is it safe to assume strict comparison in a JavaScript switch statement?
- Java break statement with switch
- Explain the finally Statement in JavaScript with examples.
- Java switch statement with multiple cases.

Advertisements