www.webdeveloper.com
Results 1 to 10 of 10

Thread: switch question

  1. #1
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787

    switch question

    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 "==" ?

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    Why shouldn't case 3 alert?

    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)

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by Declan1991
    Why shouldn't case 3 alert?
    because 3 === true is false!

  4. #4
    Join Date
    Aug 2007
    Posts
    3,767
    That's within an alert, it's irrelevant what it evaluates to.
    Code:
    var b=[1,2,3,4,5]
    
    switch(true){
      case b.length>4: alert(true===b.length>4); 
      case 3: true===3;
      default: alert("default reached");
    }
    is what you are looking for.

  5. #5
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    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.

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    thanks for the response, but i am still confused.

    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.

    what am i missing here?

  7. #7
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    it should not matter what happens on the right side of the ":".
    Apparently it does and the ECMA spec agrees with the behavior from what I can understand.

  8. #8
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    I just looked it up in "JavaScript - The Definitive Guide" and it also explains this behavior. So we can safely assume that this is by design.

    Whether the design is wrong or not is another story...
    Last edited by Dok; 09-10-2008 at 08:24 PM.

  9. #9
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    thank you DOK, that makes sense.
    well, actually it doesn't make sense, but you know what i mean...

    glad to know i am not crazy, or at least, not because of that!

    thanks again for such an exhaustive reply, and digging around to come to a definitive guideline, no matter how counter-intuitive it may be.

    remember: "just in case, always take a break"

  10. #10
    Join Date
    Aug 2007
    Posts
    3,767
    I missed the point of the question alright. I don't think it's unintuitive though, or maybe I just learned it the right way first.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles