# Switch statments

**URL:** <https://forum.freecodecamp.org/t/switch-statments/125896>\
**Category:** General\
**Tags:** switch\
**Created:** [June 10, 2017, 8:44pm UTC](https://forum.freecodecamp.org/t/switch-statments/125896 "2017-06-10T20:44:37Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomer-n](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tomer-n/32/71826_2.png) [@tomer-n](https://forum.freecodecamp.org/u/tomer-n)\
**Post date:** [June 10, 2017, 8:44pm UTC](https://forum.freecodecamp.org/t/switch-statments/125896/1 "2017-06-10T20:44:37Z")

</div>

hey,

i am trying to run this code but i get nothing in the console, no output and no error:

---

<div class="post-metadata">

**Author:** ![fentablar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fentablar/32/41967_2.png) [@fentablar](https://forum.freecodecamp.org/u/fentablar)\
**Post date:** [June 10, 2017, 8:59pm UTC](https://forum.freecodecamp.org/t/switch-statments/125896/2 "2017-06-10T20:59:17Z")

</div>

well without the context it’s difficult to know what you’re expecting, but in any case you are passing “a” to your switch statement, which means that any ‘case’ you use would reference whatever the value of “a” is.

Such that, your “case 1” specifically translates to “if (a === 1)”. So, it would be impossible for your operation “if(a\>1)” to ever be true because that operation would only be attempted if “a” were to equal 1, as per your case definition.

---

<div class="post-metadata">

**Author:** ![tomer-n](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tomer-n/32/71826_2.png) [@tomer-n](https://forum.freecodecamp.org/u/tomer-n)\
**Post date:** [June 11, 2017, 5:44am UTC](https://forum.freecodecamp.org/t/switch-statments/125896/3 "2017-06-11T05:44:09Z")

</div>

hey @fentablar thanks for replying!

I am trying to write a simple code with switch that has 2 cases. the first is a situation when a\>1 and the other is default. is there a way to enter if statement inside the switch case?

when i write case 1: it means that this is a case where “a” is equal to 1 ?

---

<div class="post-metadata">

**Author:** ![fentablar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fentablar/32/41967_2.png) [@fentablar](https://forum.freecodecamp.org/u/fentablar)\
**Post date:** [June 11, 2017, 5:49am UTC](https://forum.freecodecamp.org/t/switch-statments/125896/4 "2017-06-11T05:49:42Z")

</div>

> [@tomer-n](#):
>
> when i write case 1: it means that this is a case where “a” is equal to 1 ?

Correct. It may behove you to check out MDN’s documentation on switch: [switch - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch)

---

<div class="post-metadata">

**Author:** ![tomer-n](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tomer-n/32/71826_2.png) [@tomer-n](https://forum.freecodecamp.org/u/tomer-n)\
**Post date:** [June 11, 2017, 6:21pm UTC](https://forum.freecodecamp.org/t/switch-statments/125896/5 "2017-06-11T18:21:26Z")

</div>

thanks!

so basically i can’t use if statement in a case like this:

case if (a\>1):

---

<div class="post-metadata">

**Author:** ![owel](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/owel/32/48425_2.png) [@owel](https://forum.freecodecamp.org/u/owel)\
**Post date:** [June 11, 2017, 6:40pm UTC](https://forum.freecodecamp.org/t/switch-statments/125896/6 "2017-06-11T18:40:09Z")

</div>

```auto
switch (a) {
   case 1: { do something } 
 } // end case 

```

is the equivalent of:

```auto
if (a==1) {
   {do something}
} 

```

If you need to use comparison operators like \<=, \>=, etc… you need to use an if( ) statement.

---

<div class="post-metadata">

**Author:** ![Aldek](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/aldek/32/54462_2.png) [@Aldek](https://forum.freecodecamp.org/u/Aldek)\
**Post date:** [June 11, 2017, 11:51pm UTC](https://forum.freecodecamp.org/t/switch-statments/125896/7 "2017-06-11T23:51:45Z")

</div>

> [@tomer-n](#):
>
> case 1: if(a\>1)  
> ann = “delta”;  
> break;

You would need to use  
case (a\>1) :  
ann = “delta”;  
break;

(At least as far as my knowledge of switch statements goes).

---

<div class="post-metadata">

**Author:** ![owel](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/owel/32/48425_2.png) [@owel](https://forum.freecodecamp.org/u/owel)\
**Post date:** [June 12, 2017, 12:15am UTC](https://forum.freecodecamp.org/t/switch-statments/125896/8 "2017-06-12T00:15:37Z")

</div>

No, that doesn’t work… JS will complain of Invalid or unexpected token. Try it in your console.

Now, you can have a comparison in your switch, but your case statements will be true or false

```auto
switch (5<4){
    case true: console.log('so true');
        break;
    case false: console.log('nope');
        break;
           }

```

---

<div class="post-metadata">

**Author:** ![fentablar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fentablar/32/41967_2.png) [@fentablar](https://forum.freecodecamp.org/u/fentablar)\
**Post date:** [June 12, 2017, 3:01am UTC](https://forum.freecodecamp.org/t/switch-statments/125896/9 "2017-06-12T03:01:13Z")

</div>

actually you could do:

```auto
switch (true) {
  case a > 1:
    //do something;
    break;
  default:
    //default action;
}

```

again, refer to MDN – [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch#Can\_I\_use\_switch\_to\_simplify\_multiple\_if’s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch#Can_I_use_switch_to_simplify_multiple_if's)

---

<div class="post-metadata">

**Author:** ![owel](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/owel/32/48425_2.png) [@owel](https://forum.freecodecamp.org/u/owel)\
**Post date:** [June 12, 2017, 3:03am UTC](https://forum.freecodecamp.org/t/switch-statments/125896/10 "2017-06-12T03:03:32Z")

</div>

I’ll be dipped… never used that. Thanks for the info!

---

<div class="post-metadata">

**Author:** ![fentablar](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fentablar/32/41967_2.png) [@fentablar](https://forum.freecodecamp.org/u/fentablar)\
**Post date:** [June 12, 2017, 3:09am UTC](https://forum.freecodecamp.org/t/switch-statments/125896/11 "2017-06-12T03:09:06Z")

</div>

yeah – when I first read that, I thought to myself “ok, but why would anyone ever actually do that”, but then I thought about the possibilities if you don’t use breaks in the cases and could trigger multiple non-related operations with a single switch

---

<div class="post-metadata">

**Author:** ![owel](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/owel/32/48425_2.png) [@owel](https://forum.freecodecamp.org/u/owel)\
**Post date:** [June 12, 2017, 3:14am UTC](https://forum.freecodecamp.org/t/switch-statments/125896/12 "2017-06-12T03:14:51Z")

</div>

Yeah, it seems counter-intuitive. Like saying

```auto
if (true) {
    if (a >1) { 
        // do something
    }
    if (a <1) {
       // do something
    }
}

```

… might as well do if…then in the first place.

But If one removes the breaks to let it fall down like waterfall, it might have some use case scenario.  
But me I think it’s fertile ground for program logic bugs.

---

<div class="post-metadata">

**Author:** ![AJ0201](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/aj0201/32/8058_2.png) [@AJ0201](https://forum.freecodecamp.org/u/AJ0201)\
**Post date:** [June 12, 2017, 5:16am UTC](https://forum.freecodecamp.org/t/switch-statments/125896/13 "2017-06-12T05:16:20Z")

</div>

Aren’t switch statements used in Ruby?

---

<div class="post-metadata">

**Author:** ![codyseibert](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codyseibert/32/62395_2.png) [@codyseibert](https://forum.freecodecamp.org/u/codyseibert)\
**Post date:** [June 23, 2017, 4:01pm UTC](https://forum.freecodecamp.org/t/switch-statments/125896/14 "2017-06-23T16:01:02Z")

</div>

It seems like you are trying to put an if statement into your switch statement, which is invalid syntax. I created a video on flow control on youtube if you are interested in watching another reference regarding switch cases:

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:55pm UTC](https://forum.freecodecamp.org/t/switch-statments/125896/15 "2021-01-17T20:55:23Z")

</div>

Closed on January 17, 2021, 8:55pm UTC


