125

This code does not work in internet explorer. Any alternative?

"abcde".includes("cd")
WestLangley
  • 102,557
  • 10
  • 276
  • 276
Carlosss
  • 1,313
  • 2
  • 10
  • 12

11 Answers11

152

String.prototype.includes is, as you write, not supported in Internet Explorer (or Opera).

Instead you can use String.prototype.indexOf. #indexOf returns the index of the first character of the substring if it is in the string, otherwise it returns -1. (Much like the Array equivalent)

var myString = 'this is my string';
myString.indexOf('string');
// -> 11

myString.indexOf('hello');
// -> -1

MDN has a polyfill for includes using indexOf: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill

EDIT: Opera supports includes as of version 28.

EDIT 2: Current versions of Edge supports the method. (as of 2019)

Phillip
  • 6,033
  • 3
  • 24
  • 34
  • Is include() the only function which is not supported by IE? Or there are other typescript or JavaScript functions which are not supported by IE? – Abdullah Feroz Jul 10 '18 at 16:51
  • 10
    If we need a `Boolean`, we can `(myString.indexOf('string') > -1) // to get a boolean true or false` – Aakash Nov 14 '18 at 05:27
39

Or just put this in a Javascript file and have a good day :)

String.prototype.includes = function (str) {
  var returnValue = false;

  if (this.indexOf(str) !== -1) {
    returnValue = true;
  }

  return returnValue;
}
Ben Thomas
  • 3,180
  • 2
  • 20
  • 38
Prasun
  • 391
  • 3
  • 2
11

includes() is not supported by most browsers. Your options are either to use

-polyfill from MDN https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes

or to use

-indexof()

var str = "abcde";
var n = str.indexOf("cd");

Which gives you n=2

This is widely supported.

deeveeABC
  • 960
  • 3
  • 12
  • 34
  • If you use the polyfill from MDN, _do not iterate your string with `for...in`!_, it will iterate over `String.prototype.includes` if you define it like that. – Patrick Roberts Feb 23 '18 at 20:29
9

Problem:

Try running below(without solution) from Internet Explorer and see the result.

console.log("abcde".includes("cd"));

Solution:

Now run below solution and check the result

if (!String.prototype.includes) {//To check browser supports or not
  String.prototype.includes = function (str) {//If not supported, then define the method
    return this.indexOf(str) !== -1;
  }
}
console.log("abcde".includes("cd"));
Sastrija
  • 3,284
  • 6
  • 47
  • 64
4

This one may be better and shorter:

function stringIncludes(a, b) {
    return a.indexOf(b) >= 0;
}
Skwall
  • 41
  • 5
3

I had the same problem when working in Angular 5. In order to make it work directly without writing a polyfill yourself, just add the following line to polyfills.ts file:

import "core-js/es7/array"

Also, tsconfig.json lib section might be relevant:

"lib": [
  "es2017",
  "dom"
],
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
3

For react:

import 'react-app-polyfill/ie11';
import 'core-js/es5';
import 'core-js/es6';
import 'core-js/es7';

Issue resolve for - includes(), find(), and so on..

1

If you want to keep using the Array.prototype.include() in javascript you can use this script: github-script-ie-include That converts automatically the include() to the match() function if it detects IE.

Other option is using always thestring.match(Regex(expression))

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
mikeTeixeira88
  • 163
  • 2
  • 8
1

It works for me:

function stringIncludes(a, b) {
      return a.indexOf(b) !== -1;
}
Abel Valdez
  • 2,368
  • 1
  • 16
  • 33
0

this is because ie does not support includes so Make a dot function and use it just like es6 includes() in es5 as below :

String.prototype.includes = function (str) {
 return this.indexOf(str) !== -1;
}

Following is the string

var myString = 'this is my string';

Checking the match as following:

console.log(myString.includes('string')); // true
console.log(myString.includes('street')); //false

Now you can add this for ES5 using same indexOf in includes way

Parameshwar
  • 856
  • 8
  • 16
-1

You can do the same with !! and ~ operators

 var myString = 'this is my string';

 !!~myString.indexOf('string');
 // -> true

 !!~myString.indexOf('hello');
 // -> false

here's the explanation of the two operators (!! and ~ )

What is the !! (not not) operator in JavaScript?

https://www.joezimjs.com/javascript/great-mystery-of-the-tilde/

mesvil
  • 37
  • 4