Can anyone explain to me why first one is not working and second one is working?
First Statement
function test(n) {
switch (n) {
case (n
switch uses strict comparison.
You take a number in the switch statement and in cases, just comparsions which return a boolean value.
A switch statement first evaluates its expression. It then looks for the first
case
clause whose expression evaluates to the same value as the result of the input expression (using strict comparison,===
) and transfers control to that clause, executing the associated statements. (If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.) If no matchingcase
clause is found, the program looks for the optionaldefault
clause, and if found, transfers control to that clause, executing the associated statements. If nodefault
clause is found, the program continues execution at the statement following the end ofswitch
. By convention, thedefault
clause is the last clause, but it does not need to be so.