Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS - array.includes issue

I have this issue with a JS function array.includes. I have this array:

The thing is when I use this code, nothing will happen.

var array_type;
//array has these 2 values:
//array_type[0] == 0;
//array_type[1] == 2;
if (array_type.includes(2)) {
 console.log("good");
}

Do you have any idea why? Thank you for any help.

like image 675
Adam Šulc Avatar asked Dec 11 '25 08:12

Adam Šulc


2 Answers

If you are using Internet Explorer then array.includes() will not work. Instead you need to use indexOf. Internet Explorer doesn't have a support for Array.includes()

var array_type = [0, 2];

if (array_type.indexOf(2) !== -1) {
  console.log("good");
}

References for includes()

References for indexOf()

Check the browser compatibility sections in the link

like image 90
Sanchit Patiyal Avatar answered Dec 14 '25 00:12

Sanchit Patiyal


This code works

[1,2].includes(2)

but you have to be careful if you can use the includes function

https://caniuse.com/#search=includes

like image 40
juan garcia Avatar answered Dec 13 '25 23:12

juan garcia