1. 2

    The object literal pattern only supports switching on strings, because only strings can be object keys. By contrast, JavaScript’s built-in switch statement can switch on numbers too. Though with JavaScript’s type coercion, maybe you could use strings like '39' as object keys and access it with the index 39, but that feels unsafe to me.

    1. 2

      It might “feel” unsafe, but it’s perfectly fine. Here, try this:

      var x = {"39": 13.37}
      print(x[39])
      
      1. 3

        I think just further demonstrates why people don’t trust Javascript: to many reasonable, seasoned programmers that shouldn’t work, but it does.

        1. 3

          This works because JavaScript converts the hash keys to string before using it as key:

          > a = 'key';  // String
          > b = 4;  // Number
          > c = {};  // Object
          > d = {};
          > d[a] = 1;
          > d[b] = 2;
          > d[c] = 3;
          > d
          { '4': 2,
            key: 1,
            '[object Object]': 3 }
          

          This means that even though you can use objects as keys, they will all translate to '[object Object]'.