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
}
  • (A) ===
  • (B) equal
  • (C) equals
  • (D) ==
πŸ’¬ Discuss
βœ… 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.

Explanation by: Mr. Dubey

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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
295
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Kirti
Publisher
πŸ“ˆ
87%
Success Rate