# switch / case as expression

**URL:** <https://es.discourse.group/t/switch-case-as-expression/194>\
**Category:** 💡 Ideas\
**Created:** [January 16, 2020, 11:57pm UTC](https://es.discourse.group/t/switch-case-as-expression/194 "2020-01-16T23:57:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bart](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bart/32/219_2.png) [@bart](https://es.discourse.group/u/bart)\
**Post date:** [January 16, 2020, 11:57pm UTC](https://es.discourse.group/t/switch-case-as-expression/194/1 "2020-01-16T23:57:05Z")

</div>

Hello!

Sometimes I’m rubbing my hands in enjoyment over a nice `return` statement I’m writing in a React component, only to realise that I need to write a function for the sole purpose of using a switch/case block in the expression following `return`. What if, just like [`throw`](https://github.com/tc39/proposal-throw-expressions), `switch`/`case` could be an expression instead?

One possible implementation would be to wrap `switch` inside an [immediately invoked function expression](https://en.wikipedia.org/wiki/Immediately_invoked_function_expression), i.e., an implicit iife, if you will:

```javascript
// input
const fn = () => {
  return switch (a) {
    case 1:
      return 'hello'

    case 2:
      return 'world'
  }
}

```

```javascript
// output
const fn = () => {
  return (() => {
    switch (a) {
      case 1:
        return 'hello'

      case 2:
        return 'world'
    }
  })()
}

```

Why this change, if we can use an IIFE instead? Because I think they’re a bit ugly, syntax-wise, and perhaps confusing to the uninitiated; whereas the new syntax looks more orderly and is more convenient to use inside expressions.

Thanks in advance for your feedback!

---

<div class="post-metadata">

**Author:** ![jridgewell](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jridgewell/32/18_2.png) [@jridgewell](https://es.discourse.group/u/jridgewell)\
**Post date:** [January 17, 2020, 12:12am UTC](https://es.discourse.group/t/switch-case-as-expression/194/2 "2020-01-17T00:12:42Z")

</div>

I think this is covered by [Do Expressions](https://github.com/tc39/proposal-do-expressions).

---

<div class="post-metadata">

**Author:** ![Jonas\_Wilms](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jonas_wilms/32/189_2.png) [@Jonas\_Wilms](https://es.discourse.group/u/Jonas_Wilms)\
**Post date:** [January 23, 2020, 6:38pm UTC](https://es.discourse.group/t/switch-case-as-expression/194/3 "2020-01-23T18:38:32Z")

</div>

I think that the syntax you propose would be possible, as `switch` is currently a syntax error inside expression context. However I'm not quite sure that `return` should be used to determine the expression the `switch` expression would evaluate to, as (a) it would make it impossible to return regularily, and (b) it would make it hard to determine what `return` returns to.

---

<div class="post-metadata">

**Author:** ![pygy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/pygy/32/231_2.png) [@pygy](https://es.discourse.group/u/pygy)\
**Post date:** [January 23, 2020, 7:13pm UTC](https://es.discourse.group/t/switch-case-as-expression/194/4 "2020-01-23T19:13:49Z")

</div>

Yup, specifically

```javascript
const x = do {
  switch (a) {
    case 1: 'hello'; break;
    case 2: 'world'; break;
  }
}

```

You can try it out in the Browser console: paste this, it outputs `5`:

```javascript
switch(true) {
    case true: 5; break;
    case false: 6; break;
}

```

Do expressions use the same logic.

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [January 24, 2020, 8:15am UTC](https://es.discourse.group/t/switch-case-as-expression/194/5 "2020-01-24T08:15:03Z")

</div>

Also: [https://github.com/tc39/proposal-pattern-matching](https://github.com/tc39/proposal-pattern-matching)

Your initial `fn` might look like this using that proposal:

```javascript
const fn = () =>
    case (a) {
        when 1 -> 'hello',
        when 2 -> 'world',
    }

```

---

<div class="post-metadata">

**Author:** ![djwashburn](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@djwashburn](https://es.discourse.group/u/djwashburn)\
**Post date:** [May 22, 2020, 3:10pm UTC](https://es.discourse.group/t/switch-case-as-expression/194/6 "2020-05-22T15:10:03Z")

</div>

[Java has recently added a switch expression](https://docs.oracle.com/en/java/javase/13/language/switch-expressions.html) which solves many of the common issues with switch statements. I suggest we look at using it as a model. Benefits include an enforced default, not needing to worry about break statements and fallthrough, evaluating as an expressoin, and each branch being its own block scope.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [May 22, 2020, 3:57pm UTC](https://es.discourse.group/t/switch-case-as-expression/194/7 "2020-05-22T15:57:54Z")

</div>

All of these are already part of the pattern matching proposal.
