# Flexible switch statements using arrow functions

**URL:** <https://es.discourse.group/t/flexible-switch-statements-using-arrow-functions/525>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [November 4, 2020, 9:48am UTC](https://es.discourse.group/t/flexible-switch-statements-using-arrow-functions/525 "2020-11-04T09:48:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [November 4, 2020, 9:48am UTC](https://es.discourse.group/t/flexible-switch-statements-using-arrow-functions/525/1 "2020-11-04T09:48:22Z")

</div>

Hi everyone,  
Switch statements in JavaScript are currently limited in functionality. We don't often see them in code (except in Redux applications).  
They are just good for equality compare.  
I'm just suggesting an add-on to their functionality.  
**syntax**

```javascript
switch((x, y) => x < y) {
    case 1, 2 : /*do something*/;
    break;
    case 10, 5 : /*do something*/;
    break;
    default: console.log("Something's not right");
}

class Element {}
switch(x => Element instanceof x) {
   case React.Component: /*do something*/;
   break;
   case customElements: /*do something*/;
   break;
   case HTMLElement: /*do something*/;
   break;
   case Error: /*do something*/;
   default: console.log("Yay, whatever! ");
}
//etc, etc...

```

This seems like the **match** statement in Rust right??  
However there should be some restrictions upon the function passed to the switch.  
_It must be an arrow function with implicit return_  
i.e,

```javascript
//The following invalid switch statements :( will result in an error
switch(x => { return !!x }) {
/*
     switch body
*/
}

switch(function(x) { return !!x }) {
/*
     switch body
*/
}

```

Alright, what does everyone think about this??  
What are the pros and cons??  
Please mention!

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [November 4, 2020, 1:24pm UTC](https://es.discourse.group/t/flexible-switch-statements-using-arrow-functions/525/2 "2020-11-04T13:24:48Z")

</div>

For your use case, an `if`/`else` chain seems more appropriate, as the repeated tests would be what your proposed switch statement desugars to anyway. It's also shorter and doesn't need `break` statements to avoid fallthrough:

```javascript
if (1 < 2) { /* do something */; }
else if (10 < 5) { /* do something */; }
else { console.log("Something's not right"); }

```

```javascript
if (Element instanceof React.Component) { /*do something*/; }
else if (Element instanceof customElements) { /*do something*/; }
else if (Element instanceof HTMLElement) { /*do something*/; }
else if (Element instanceof Error) { /*do something*/; }
else { console.log("Yay, whatever! "); }

```

The power of `switch` is in optimisations that are only possible with equality comparisons.

> [@jithujoshyjy](#):
>
> This seems like the **match** statement in Rust right?

For that, see the [_pattern matching_ proposal](https://github.com/tc39/proposal-pattern-matching).
