 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we have a return statement in a JavaScript switch statement?
The JavaScript switch statement can contain return statements if it is present inside a function. The function will return the value in the switch statement and the code after the switch statement will not be executed.
Following is the code to have return statements 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>Return statement in JavaScript switch</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");    function returnDay(val) {       switch (val) {          case 1:             return "It's monday";          case 2:             return "It's tuesday";          case 3:             return "It's wednesday";          case 4:             return "It's thursday";          case 5:             return "It's friday";          case 6:             return "It's saturday";          case 7:             return "It's sunday";          default:             return "Enter a value between 1 - 7";       }    }    document.querySelector(".Btn").addEventListener("click", () => {       resEle.innerHTML = returnDay(parseInt(dayVal.value));    }); </script> </body> </html>  Output
The above code will produce the following output −

On entering a number between 1-7 and clicking on CHECK −

Advertisements
 