.includes() not working in Internet Explorer

Javascript

Javascript Problem Overview


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

"abcde".includes("cd")

Javascript Solutions


Solution 1 - Javascript

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)

Solution 2 - Javascript

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;
}

Solution 3 - Javascript

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.

Solution 4 - Javascript

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"));

Solution 5 - Javascript

This one may be better and shorter:

function stringIncludes(a, b) {
	return a.indexOf(b) >= 0;
}

Solution 6 - Javascript

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"
],

Solution 7 - Javascript

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..

Solution 8 - Javascript

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))

Solution 9 - Javascript

It works for me:

function stringIncludes(a, b) {
      return a.indexOf(b) !== -1;
}

Solution 10 - Javascript

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

Solution 11 - Javascript

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 ~ )

https://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCarlosssView Question on Stackoverflow
Solution 1 - JavascriptPhillipView Answer on Stackoverflow
Solution 2 - JavascriptPrasunView Answer on Stackoverflow
Solution 3 - JavascriptdeeveeABCView Answer on Stackoverflow
Solution 4 - JavascriptJoeView Answer on Stackoverflow
Solution 5 - JavascriptSkwallView Answer on Stackoverflow
Solution 6 - JavascriptAlexei - check CodidactView Answer on Stackoverflow
Solution 7 - Javascriptniket bajajView Answer on Stackoverflow
Solution 8 - JavascriptmikeTeixeira88View Answer on Stackoverflow
Solution 9 - JavascriptAbel ValdezView Answer on Stackoverflow
Solution 10 - JavascriptParameshwarView Answer on Stackoverflow
Solution 11 - JavascriptmesvilView Answer on Stackoverflow