# String in method - Array includes method

**URL:** <https://es.discourse.group/t/string-in-method-array-includes-method/495>\
**Category:** 💡 Ideas\
**Created:** [October 5, 2020, 8:06pm UTC](https://es.discourse.group/t/string-in-method-array-includes-method/495 "2020-10-05T20:06:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matias](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matias](https://es.discourse.group/u/Matias)\
**Post date:** [October 5, 2020, 8:06pm UTC](https://es.discourse.group/t/string-in-method-array-includes-method/495/1 "2020-10-05T20:06:25Z")

</div>

We have enjoyed the clarity of includes for array, like this:

var fruits = [banana, cherry, apple, arape];  
if (fruits.includes("banana"))  
...

But, when we need to test a group, i propose to use in that way:

var myStringFruit = "banana";  
if (myStringFruit.in(fruits))  
...

if (myStringFruit.in(["apple", "grape", "peach", "pomelo"]))  
...

this is a more elegant solution of the traditional  
if (myStringFruit=="apple" || myStringFruit=="grape" || myStringFruit=="peach".....

¿what you think?

---

<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 5, 2020, 8:26pm UTC](https://es.discourse.group/t/string-in-method-array-includes-method/495/2 "2020-10-05T20:26:04Z")

</div>

Why would we need `myStringFruit.in(fruits)` when we have `fruits.includes(myStringFruit)`? That seems like it'd be adding an extra thing to the language (a very expensive proposition) solely so "collection includes item" could be written in reverse, as "item is in collection".

---

<div class="post-metadata">

**Author:** ![Matias](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matias](https://es.discourse.group/u/Matias)\
**Post date:** [October 5, 2020, 8:59pm UTC](https://es.discourse.group/t/string-in-method-array-includes-method/495/3 "2020-10-05T20:59:22Z")

</div>

Yes friend, you right, but, for clarify effects, the method "in" in the String class would be nice,

["banana", "apple", "cherry"].includes(myStringFruit)

can be more clarify in this too:

myStringFruit.in(["banana", "apple", "cherry"]);

---

<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:** [October 5, 2020, 9:46pm UTC](https://es.discourse.group/t/string-in-method-array-includes-method/495/4 "2020-10-05T21:46:41Z")

</div>

I don't think an `in` method provides more clarity than the reverse. In addition, it seems to be on the wrong side - checking for existence is the responsibility of the collection, not of any arbitrary values:

- `value.in(collection)` should work for a `collection` that is an array, a set, an iterator, etc. It would need to dispatch to the collection-specific method anyway (using a `Symbol` approach, similar to `String`'s `split` and `match` methods supporting arbitrary patterns)
- `value.in(collection)` should work for arbitrary `value`s, including `null`, `undefined` and `Object.create(null)`, which JavaScript is not designed to support. It seems you are proposing an `in` method only for strings?!

---

<div class="post-metadata">

**Author:** ![Matias](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Matias](https://es.discourse.group/u/Matias)\
**Post date:** [October 5, 2020, 10:05pm UTC](https://es.discourse.group/t/string-in-method-array-includes-method/495/5 "2020-10-05T22:05:16Z")

</div>

seems right bergus, i only want to clarify like sql sintax, but I agree that it can be solved in reverse.  
in sql familiar sintax clarify  
...  
WHERE fruit IN ( ..... a list of elements)  
I found it interesting to propose it  
a single element (string) is in a group (Array or Set or Map)  
Can be usefull with Numbers too looks this

var myNumer = 123;  
if myNumer==11 || myNumer==22 || myNumber==54 || myNumber==55....

this can be solved simplifyg in this form:

myNumber.in(11,22,54,55)

avoiding wrong writing (the variable is writed only once)
