# If statement value guard shorthand

**URL:** <https://es.discourse.group/t/if-statement-value-guard-shorthand/314>\
**Category:** I have questions\
**Created:** [April 23, 2020, 7:21pm UTC](https://es.discourse.group/t/if-statement-value-guard-shorthand/314 "2020-04-23T19:21:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danm](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/danm/32/59_2.png) [@danm](https://es.discourse.group/u/danm)\
**Post date:** [April 23, 2020, 7:21pm UTC](https://es.discourse.group/t/if-statement-value-guard-shorthand/314/1 "2020-04-23T19:21:26Z")

</div>

Sorry about my terminology, this pattern probably exist in other languages and I just don't know the name of it.

Simply, if I want to create an if statement where a value must match 1 other value in a list.

```javascript
const val = "big"; 
if (val === "tiny" || val === "small" || val === "medium" || val === "large") {
  // valid size
}

```

There are a bunch of SO articles out there asking what the best solution is and there are many variations. But could something be built into the language eventually? I'm sure it has been discussed before, what were the outcomes?  
Ideally, it could look something like ...

```javascript
const val = "big"; 
if (val ==~ "tiny" || "small" || "medium" || "large") {
  // valid size
}

```

---

<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:** [April 23, 2020, 7:40pm UTC](https://es.discourse.group/t/if-statement-value-guard-shorthand/314/2 "2020-04-23T19:40:38Z")

</div>

There are as many variations of the question as there are solutions. The outcomes of prior syntax proposals were that the existing solutions are _good enough_ - and not worth the hassle of introducing new syntax. With ES6 specifically, you can write

```javascript
if (["tiny", "small", "medium", "large"].includes(val))

```

or

```javascript
if (new Set(["tiny", "small", "medium", "large"]).has(val))

```

and the most concise solution for strings is probably

```javascript
if (/^(tiny|small|medium|large)$/.test(val))

```

---

<div class="post-metadata">

**Author:** ![danm](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/danm/32/59_2.png) [@danm](https://es.discourse.group/u/danm)\
**Post date:** [April 23, 2020, 7:55pm UTC](https://es.discourse.group/t/if-statement-value-guard-shorthand/314/3 "2020-04-23T19:55:57Z")

</div>

OK, good to know it was considered.  
Absolutely no shortage of different approaches to the problem and thanks for the preferred examples.  
Thanks!
