Switch case as string

The reason you’re seeing this behavior is that this within the each call is a String object instance, not a string primitive. JavaScript has both. In a switch statement, the comparison with the cases is via ===, and a string instance is not === to a string primitive.

Three ways to fix it:

  1. If you change your switch to:

    switch (String(this)) {
    

    …that will turn it back into a primitive, whereupon your switch works.

  2. As VisioN points out in the comments below, use the arguments that $.each passes (each string — as a primitive — will be provided as the second argument):

    $.each(typ, function(index, value) {
        switch (value) {
            // ...
        }
    });
    
  3. Use any of the alternatives discussed in this other answer (one of which is a nice simple for loop).


Side note: You’re falling prey to The Horror of Implicit Globals by not declaring your typ variable.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)