# A new conditioning way, compare multiple right hand values.

**URL:** <https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [October 24, 2023, 6:13am UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832 "2023-10-24T06:13:31Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nik](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nik/32/1891_2.png) [@Nik](https://es.discourse.group/u/Nik)\
**Post date:** [October 24, 2023, 6:13am UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/1 "2023-10-24T06:13:31Z")

</div>

Hi fellows,  
Many times I have experienced the case where I have to check the same variable for different values, and when the variable is long, it gets awkward and the code does not look well.

For example:

```javascript
if(objectWithVeryLongName.itemWithVeryLongName == 1 || objectWithVeryLongName.itemWithVeryLongName == 2 || objectWithVeryLongName.itemWithVeryLongName == 3) ....

```

I usually do something like:

```javascript
if([1,2,3].includes(objectWithVeryLongName.itemWithVeryLongName)) .... 

```

but is a bit hacky, and also I can't get advantage from the type comparing (==, ===). Sometimes maybe I'll want to check for string or number to be present in the variable, in this case I'll have to go even hackyer. And yes, I know I can use [test] regexp... still hacky!

I want this:

```javascript
if(objectWithVeryLongName.itemWithVeryLongName == (1 || 2 || 3)) ....

```

This is what the console shows (for the people that claim that this is working in JS), it is not throwing error but gets only the first value under consideration.

```javascript
1 == (1 || 2 || 3) // true
2 == (1 || 2 || 3) // false
3 == (1 || 2 || 3) // false

```

Thank you for your attention :)

---

<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:** [October 24, 2023, 6:24am UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/2 "2023-10-24T06:24:46Z")

</div>

That particular syntax is already valid and can't be changed.

[https://github.com/tc39/proposal-pattern-matching](https://github.com/tc39/proposal-pattern-matching) may be a good solution for this.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [October 24, 2023, 6:10pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/3 "2023-10-24T18:10:49Z")

</div>

Some other threads:

> [@Comparison to multiple values syntactic sugar](https://es.discourse.group/t/comparison-to-multiple-values-syntactic-sugar/357):
>
> In ECMA I would use (variable === val1 || variable === val2 ...) to attempt to compare a variable to multiple values. I think it would be a lot more elegant to run variable === | val1, val 2 | and have it automatically expand.

> [@Chained comparison syntax](https://es.discourse.group/t/chained-comparison-syntax/148):
>
> Would it be a good idea to introduce a chained comparison syntax? For example, [Python has them](https://docs.python.org/3/reference/expressions.html#comparisons).

> [@Operators: ||==, ||===](https://es.discourse.group/t/operators/1577):
>
> Could we use chaining comparison operators? Multiple comparisons: if( a === "first-comparison" || a === "second-comparison" || a === "third-comparison ) ... Chained comparisons: if( a ||=== "first-comparison ||=== "second-comparison" ||=== "third-comparison" ) ... The same syntax would work for &&. This is such a common issue that I assume it has already been proposed and rejected, but I couldn't find the conversation. The stack overflow question is 10 years old with 100k views. …

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [October 25, 2023, 3:07am UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/4 "2023-10-25T03:07:02Z")

</div>

> if([1,2,3].includes(objectWithVeryLongName.itemWithVeryLongName)) ....

I don't find this to be hacky at all, in fact I'd encourage the use of that pattern, I find it to be very readable.

It's unfortunate that Typescript doesn't handle Inference very well when you use that pattern, but that's a Typescript problem, not a JavaScript one. There have been attempts to ask for something like this, but the response is basically "it would require adding support for a new way to describe a type to make this work, and at this time they don't feel it's worth implementing it, partly due to the complexity cost it would put on the Typescript codebase" (see the end of [.includes or .indexOf does not narrow the type · Issue #36275 · microsoft/TypeScript · GitHub](https://github.com/microsoft/TypeScript/issues/36275#issue-551694349))

---

<div class="post-metadata">

**Author:** ![Nik](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nik/32/1891_2.png) [@Nik](https://es.discourse.group/u/Nik)\
**Post date:** [October 25, 2023, 7:41pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/5 "2023-10-25T19:41:03Z")

</div>

Yeah, I know it's a good pattern, this is what I am using.... because there is no what I want. And if to create array and use includes() function to have checked 2~3 value is not hacky, I don't know, then what is it 😊

The problem is not Typescript, my problem is that I can't use == equality, this way is capable only of strict equality check. You maybe never experience the need of something like this, but I have many times, and it gets even hackyer when you have to cast everything to numbers, for example.

Regards!

---

<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:** [October 25, 2023, 7:53pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/6 "2023-10-25T19:53:05Z")

</div>

I'm confused; `.includes()` is already using SameValueZero semantics (`===`); what other comparisons are you trying to do?

---

<div class="post-metadata">

**Author:** ![Nik](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nik/32/1891_2.png) [@Nik](https://es.discourse.group/u/Nik)\
**Post date:** [October 25, 2023, 7:59pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/7 "2023-10-25T19:59:58Z")

</div>

I can't use == equality, this way is capable only of strict equality check (===), this works only if you are sure of the incoming data types, or we need to cast them to String or Number.

---

<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:** [October 25, 2023, 8:10pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/8 "2023-10-25T20:10:42Z")

</div>

ah, well sure, but coercing them to the proper types, or validating their types, is a good thing to do anyways.

---

<div class="post-metadata">

**Author:** ![Nik](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nik/32/1891_2.png) [@Nik](https://es.discourse.group/u/Nik)\
**Post date:** [November 1, 2023, 9:03pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/9 "2023-11-01T21:03:06Z")

</div>

That's why if I need the correct types I'll use ===, besides what I propose is not something eccentric and overengineered, it looks very JS style :) right?

There is time and case for everything, checking types of the variables is not necessarily a good thing. If this was JAVA, I'm gonna be sure that my incoming data is expected type, but this is JAVASCRIPT and its freedom is what makes it so powerful!

---

<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:** [November 1, 2023, 9:45pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/10 "2023-11-01T21:45:19Z")

</div>

Certainly this is subjective, but I indeed think that checking the types of the variables _every single code path, without exception_ is indeed necessary and a good thing. Using `==` is not very JavaScripty any more, because such looseness has not been encouraged or idiomatic for a long, long time.

---

<div class="post-metadata">

**Author:** ![Nik](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nik/32/1891_2.png) [@Nik](https://es.discourse.group/u/Nik)\
**Post date:** [November 1, 2023, 10:16pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/11 "2023-11-01T22:16:19Z")

</div>

Ok, I see you know yours, and you are trying to convince everybody that you are the right :)

Let's forget for a moment about this type check, I suppose this is a matter of personal opinion, you say it is bad, I say it's good. The truth is, this functionality exists and will never disappear from JS, and it's here to stay no matter you approve it or not.

Let's go back to the fact where creating array and use includes() function is a total hack, any further comment there?

---

<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:** [November 1, 2023, 10:29pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/12 "2023-11-01T22:29:26Z")

</div>

Nope, like other commenters, that seems perfectly reasonable to me. Pattern matching will certainly be a better choice in a future where it's available.

---

<div class="post-metadata">

**Author:** ![Nik](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nik/32/1891_2.png) [@Nik](https://es.discourse.group/u/Nik)\
**Post date:** [November 1, 2023, 10:38pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/13 "2023-11-01T22:38:40Z")

</div>

@ljharb Could you please post some example code that solves this problem with your preferred way?

Example:

```javascript
if(objectWithVeryLongName.itemWithVeryLongName == 1
  || objectWithVeryLongName.itemWithVeryLongName == 2 
  || objectWithVeryLongName.itemWithVeryLongName == 3){

  //....

}

```

Thank you!

---

<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:** [November 1, 2023, 10:51pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/14 "2023-11-01T22:51:22Z")

</div>

I think any of these are viable:

```javascript
if (
objectWithVeryLongName.itemWithVeryLongName == 1
>> objectWithVeryLongName.itemWithVeryLongName == 2
>> objectWithVeryLongName.itemWithVeryLongName == 3
) {

```

```javascript
const values = new Set([1, 2, 3]);
…
if (values.has(objectWithVeryLongName.itemWithVeryLongName)) {

```

```javascript
const values = [1, 2, 3];
…
if (values.includes(objectWithVeryLongName.itemWithVeryLongName)) {

```

```javascript
if ([1, 2, 3].includes(objectWithVeryLongName.itemWithVeryLongName)) {

```

with proposed pattern matching syntax:

```javascript
if (objectWithVeryLongName.itemWithVeryLongName is 1 or 2 or 3) {

```

---

<div class="post-metadata">

**Author:** ![Nik](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nik/32/1891_2.png) [@Nik](https://es.discourse.group/u/Nik)\
**Post date:** [November 2, 2023, 12:35am UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/15 "2023-11-02T00:35:07Z")

</div>

For the first 4 methods, I have no comments, you know why.  
For the last one, not too bad... but still, how this syntax

**`if (objectWithVeryLongName.itemWithVeryLongName is 1 or 2 or 3) {`**

is more JS looking than this one

**`if (objectWithVeryLongName.itemWithVeryLongName === 1 || 2 || 3) {`**

where in JS has (is, or), and I bet it's checking the types as well, which is half of my problem.

And lastly, your method is replaceable with 4 other currently available approaches, you just wrote them above, my suggestion from the other hand, handles one more problem that none of the 4 is able to handle ( ignore types checking 😄).

Don't get me wrong, I have no desire to be right and my suggestion comes from my needs. Honestly, if you add one more argument to includes() that ignore the types, we can shake our hands as well.

something like:  
**`if([1,2,3].includes(objectWithVeryLongName.itemWithVeryLongName, true)) {`**

---

<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:** [November 2, 2023, 12:50am UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/16 "2023-11-02T00:50:54Z")

</div>

That would be encouraging implicit coercion, which the community at large as well as the committee itself has been moving away from quite intentionally.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [November 2, 2023, 3:55am UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/17 "2023-11-02T03:55:48Z")

</div>

> Honestly, if you add one more argument to includes() that ignore the types, we can shake our hands as well.

Isn't that just as much trouble as doing explicit coercion?

E.g. say you have a variable "value" that could be a number or a string. And you want to check if it's equal to a handful of different strings. You could just do this:

```javascript
['1', '2', '3'].includes(String(value))

```

Which would solve the same use-case as using a hypothetical API that auto-coerces

```javascript
['1', '2', '3'].includes(value, { coerce: true })

```

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [November 2, 2023, 11:10am UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/18 "2023-11-02T11:10:57Z")

</div>

That wouldn't work if your search set has multiple types

```javascript
['1', 2, '3'].includes(String(value))

```

You could use some() which would let you be explicit about how you compare

```javascript
['1', 2, '3'].some(x => x == value);
['1', 2, '3'].some(x => x === value);
['1', 2, '3'].some(x => Object.is(x, value));

```

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [November 2, 2023, 3:08pm UTC](https://es.discourse.group/t/a-new-conditioning-way-compare-multiple-right-hand-values/1832/19 "2023-11-02T15:08:29Z")

</div>

This is true - guess I was assuming we were comparing against literals, where you can just make them all strings. It you're comparing with other variables, you can coerce those other variables as well when needed.

```javascript
['1', String(two), '3'].includes(String(value))

```
