JS switch housing does not work

I have a case switch statement that does not work. I checked the entry, it is valid. If the user is 1, it goes into the default value. If the user is any number, it is the default. What is wrong here? I don't know javascript at all.

switch (user) { case 1: // stuff break; case 2: // more stuff break; default: // this gets called break; } 
+6
source share
4 answers

Make sure you do not mix strings and integers.
Try:

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

The problem is data type mismatch. user fill type in integer.

+10
source

Type of dropping user variable to integer

  switch (+user) { case 1: .. // } 
+4
source

Javascript is a type. So, β€œ1” does not match 1. In your case, β€œuser” should be a number, not a string. You can do it simply:

 user = Number(user) 
+2
source

Source: https://habr.com/ru/post/918733/


All Articles