according to A re-introduction to JavaScript , about switch statements:
"The default clause is optional. You can have expressions in both the switch part and the cases if you like; comparisons take place between the two using the === operator:"
given:
Code:
var b=[1,2,3,4,5]
switch(true){
case b.length>4: alert(true===b.length>4);
case 3: alert(true===3);
default: alert("default reached");
}
why does case 3 hit?
i know the sequence is unbroken, and that's why default hits, but i cannot for the life of me explain why the 3 should; it plainly says in the alert thats it's false.
am i missing something, or is the article wrong, and comparisons made using "==" ?
The difference between == and === is type comparison. Compare true==3 and true===3. Also note the difference (brackets added for clarity)
true===(b.length>4)
Great wit and madness are near allied, and fine a line their bounds divide.
That's within an alert, it's irrelevant what it evalulates to.
You are misunderstanding the question - took me a while to understand as well.
The example is better illustrated with this
Code:
switch(true) {
case true:
alert('true');
case false:
alert('false');
default:
alert('default');
}
It should be quite clear that the alert('false') should never be executed. It is however executed.
It turns out that if no break statement is inserted the switch will fall through after the first case match even though later case matches does not match. Next example show my point
Code:
switch(true) {
case false:
alert('false');
case true:
alert('true');
case false:
alert('false');
case false:
alert('false');
default:
alert('default');
}
This will alert true, false, false, default.
This is also the behavoir for case statements which don't use expressions
Code:
switch(1) {
case 0:
alert('0');
case 1:
alert('1');
case 2:
alert('2');
default:
alert('default');
}
Output is 1, 2.
I can reproduce this in IE6+7, FF3, Safari 3, Opera 9.51 on Windows, Mac and Linux.
The ECMA standard is really cryptic on the execution on switch statements. Its on page 68 section 12.11 if you are curious.
The comparisons are indeed made using the identity operator === and not the equality operator.
The ECMA says something like (text in [] are my editing)
If [the result of a case block] is an abrupt completion then return [that result].
An abrupt completion is any completion which is not normal. A break is an abrupt completion. If no abrupt completion is met in a case block the ECMS states
For the next [case block] evaluate [all statements] of this CaseClause. If there is no such CaseClause, goto [the default block]
The way I understand this is "if there is no explicite break statement then execute the next block" as one would naturally think.
However it doesn't say anywhere that the next case value should be evaluted against the switch expression. This explains the behavior althoug it definitly seems counter intuitive.
It might be a glitch in the specs (not the first) but all browsers behave the same way.
i am not trying to do anything, it's totally contrived.
i simply use the alert to check if the case is hitting, it should not matter what happens on the right side of the ":".
the article says that "===" is used to compare cases with the switch clause.
if that's that case, 3 is not equal to true, it's only equivalent, and thus the case should not hit.
what is interesting is that if you comment out the first case, only the default hits. this fact baffles me.
maybe i am having a senoir moment, and something will click, but i have been playing with this for about an hour, and it hasn't.
i would expect that in order to hit, true must "===" 3, yet we know it doesn't.
Bookmarks