
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
Strict Comparison in JavaScript Switch Statement
The JavaScript switch statement only uses strict comparison (===) and doesn’t converts type if matches are not found using strict comparison and will immediately execute the default statement.
Following is the code for strict comparison 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 strict comparison</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 if switch performs strict comparison or not </h3> <script> let dayVal = document.querySelector(".day"); let resEle = document.querySelector(".result"); document.querySelector(".Btn").addEventListener("click", () => { switch (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 and clicking “CHECK” the default statement would be called every time due to switch always performing strict comparison −
Advertisements