xFactorSchool.com(Beta)
How to Do Examples
Python Examples
HTML Examples
Code Examples
C Code Examples
C++ Code Examples
C# Code Examples
Java Code Examples
Python Code Examples
HTML Code Examples
CSS Code Examples
JavaScript Code Examples
Online Editor
C Code Editor
C++ Code Editor
C# Code Editor
Java Code Editor
Python Code Editor
HTML Code Editor
CSS Code Editor
JavaScript Code Editor
Practice how to use switch statement in JavaScript
JavaScript Online Editor
Run Code
More JavaScript Sample Codes
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>How to use switch statement in JavaScript</title> <!--Script is added to write JavaScript code inside it--> <script> var returnMessage = ""; const today = new Date(); const dayName = today.toLocaleDateString('en-US', { weekday: 'long' }); switch (dayName) { case 'Sunday': // if today is Sunday, this code block will execute returnMessage = "Today is " + dayName; break; case 'Monday': // if today is Monday, this code block will execute returnMessage = "Today is " + dayName; break; case 'Tuesday': // if today is Tuesday, this code block will execute returnMessage = "Today is " + dayName; break; case 'Wednesday': // if today is Wednesday, this code block will execute returnMessage = "Today is " + dayName; break; case 'Thursday': // if today is Thursday, this code block will execute returnMessage = "Today is " + dayName; break; case 'Friday': // if today is Friday, this code block will execute returnMessage = "Today is " + dayName; break; default: // other wise, this code block will execute returnMessage = "Today is Saturday"; break; } function myFunction() { document.getElementById("outPut").innerHTML = returnMessage; }; </script> </head> <body> <h1>How to use switch statement in JavaScript?</h1> <button onclick="myFunction()">Click here to see result</button> <p id="outPut"></p> </body> </html>