+ 1

Can I use "or" in a switch statement of javascript?

Is the following syntax possible? var x = prompt() switch (x) { case 1 or 2 or 3 : document.write('abc'); break; case 4 or 5 or 6 : document.write('def'); break; }

24th Oct 2020, 1:31 PM
Vatsal Sharma
Vatsal Sharma - avatar
2 ответов
+ 6
No, and neither you should do it this way, try using if-else statement instead, like if([1,2,3].includes(x)) document.write("abc") else if([4,5,6].includes(x)) document.write("def")
24th Oct 2020, 1:35 PM
777
777 - avatar
+ 1
Thanks!!
24th Oct 2020, 1:40 PM
Vatsal Sharma
Vatsal Sharma - avatar