Why can't find string.includes() in IE?


(jms) #1

In one of my website pages I have this line of code:

if (username.toLowerCase().includes("admin")) {
   // do something...

and it works fine in Chrome & edge, but when checked IE it didn’t work the same way and instead it was throwing an error, saying that the "includes()" function cannot be found!

Any ideas would be appreciated.

(this.is.sparta) #2

string.includes() method is not supported by IE, so you’ll have to switch to string.indexOf()

luckily string.indexOf() is supported by all browsers out there, just it returns the first index of the start of the string to be found.

so simply replace your line of code to this:

if (username.toLowerCase().indexOf("admin") >= 0) {
   // do something...

(Yassine) #3

string.indexOf() is surely the way to go.
includes() is not as widely supported by all browsers.