Q. In the below switch syntax, the expression is compared with the case labels using which of the following operator(s)?
Code:switch(expression)
{
statements
}
β
Correct Answer: (A)
===
Explanation:
Actually, the correct answer depends on the JavaScript version.
- In modern JavaScript (ES6 and later), the switch statement uses strict equality (===) for comparison.
- In older JavaScript versions (before ES6), it used loose equality (==), allowing type coercion.
Final Answer:
β (A) === (for modern JavaScript)
Example (Modern JavaScript - === used)
let num = "5";
switch (num) {
case 5:
console.log("Matched with 5");
break;
case "5":
console.log("Matched with '5'");
break;
default:
console.log("No match");
}
Output:
Matched with '5'
β Here, only the exact type match ("5" with "5") works, proving that === is used in modern JS.