'TypeError: x.include is not a function

I'm new on JS. In this code country is variable. Results can contain multiple answers at the same time 53,110,122 etc. And for example if I results have 68 I should get it but I get this error: Uncaught TypeError: country.include is not a function

var country         = 68;
if (country.includes(68)) {
var country         = 3;
}
if (country.includes(53)) {
var country         = 138;
}

In Python we can do this with if "x" in country . Any ideas for JS?

Note: If results have 68 and 53 same time, code working.



Solution 1:[1]

includes is a function built-in for arrays. You can use this function with Arrays only or with "values of an Object" which are also an array. Before applying this function to your array which is "country", You have to confirm that whether it is array or not. for that we have a built-in function with Arrays as Array.isArray(country). This function returns true if the provided parameter is an array and vice versa. if(Array.isArray(country)) {... your checking conditions here} Here is simple exercise for include from w3school. Might be helpful. Thanks

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1