Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,819 answers

573 users

How to use switch case (multiple-choice selection) in JavaScript

5 Answers

0 votes
var n = 2;        

switch (n) 
{
    case 1:
        document.write("One");
        break;
    case 2:
        document.write("Two");
        break;
    case 3:
        document.write("Three");
        break;
} 

/*
run:

Two   

*/

 



answered Jun 20, 2015 by avibootz
0 votes
// note: no break will enter to all cases from the start case
// the start case depend on the input n, if n == 2 start case is 2 (case 2:)         
        
var n = 2;        

switch (n) 
{
    case 1:
        document.write("One <br />");
        //break;
    case 2:
        document.write("Two <br />");
        //break;
    case 3:
        document.write("Three <br />");
        //break;
    case 4:
        document.write("Four <br />");
        //break;
} 

/*
run:

Two
Three
Four   

*/

 



answered Jun 20, 2015 by avibootz
0 votes
var n = 10;        

switch (n) 
{
    case 1:
        document.write("One <br />");
        break;
    case 2:
        document.write("Two <br />");
        break;
    case 3:
        document.write("Three <br />");
        break;
    default:
        document.write("n is not 1, 2, or 3 <br />");
} 

/*
run:

n is not 1, 2, or 3  

*/

 



answered Jun 20, 2015 by avibootz
0 votes
// no output without default if n is not 1, 2, or 3
        
var n = 10;        

switch (n) 
{
    case 1:
        document.write("One <br />");
        break;
    case 2:
        document.write("Two <br />");
        break;
    case 3:
        document.write("Three <br />");
        break;
} 

/*
run:

*/

 



answered Jun 20, 2015 by avibootz
0 votes
var s = "dan";        

switch (s) 
{
    case "tom":
        document.write("Tom <br />");
        break;
    case "rachel":
        document.write("Rachel <br />");
        break;
    case "dan":
        document.write("Dan <br />");
        break;
    default:
        document.write("default case <br />");
} 

/*
run:

Dan 

*/

 



answered Jun 20, 2015 by avibootz

Related questions

3 answers 282 views
4 answers 309 views
4 answers 264 views
1 answer 154 views
1 answer 134 views
...