Javascript – JS switch case not working

javascriptswitch statement

I have a switch case statement that doesn't work. I've checked the input, it's valid. If user is 1, it goes to default. If user is any number, it defaults. What's wrong here? I don't know javascript well at all.

switch (user) {
case 1:
    // stuff
    break;
case 2:
    // more stuff
    break;
default:
    // this gets called
    break;
}

Best Answer

Make sure you are not mixing strings and integers.
Try:

switch (user) {
    case "1":
        // stuff
        break;
    case "2":
        // more stuff
        break;
    default:
        // this gets called
}