Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript problem with switch

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #153601
    javascriptnewbie
    Participant

    var a=”d”;

    switch(a);
    {
    case 10:
    document.write(“yes!!”);
    break;

    case “d”:
    document.write(“5..”);
    break;
    }

    everything seems okay but still not working :(

    #153641
    __
    Participant

    There shouldn’t be a semi-colon after the switch:

    Also note that, because of semicolon insertion, you may still run into silent failures with your brackets on the left:

    whatever ()
    {
        this doesn't always work
    }
    
    whatever (){
        always put them on the right
    }
    

    This is a personal preference and never causes any real problems except in JavaScript.

    I would also highly recommend always including a default statement in your switches (even if it simply breaks without doing anything).

    #153646
    Alen
    Participant
    function showMaxSpeed(s){
      switch(s){
        case "bmw":
          alert("300mph");
          break;
        case "ford":
          alert("250mph");
          break;
        case "dodge":
          alert("220mph");
          break;
      }
    }
    
    showMaxSpeed("ford"); // 250mph
    
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.