Nikunj Satasiya

Nikunj Satasiya

  • 275
  • 6.1k
  • 3.5m

JavaScript includes and indexOf not working in IE8

Mar 4 2021 6:38 AM
Hi Everyone, 
 
I am working with one of the projects to make it compatible with IE8. I got this issue as I have shown below, will you please give me any alternate way or suggestion to use these functions in Javascript?
 
SCRIPT438: Object doesn't support property or method 'includes'
SCRIPT436: Object doesn't support property or method 'indexOf 
 
 
I tried this  
  1. function includes(container, value) {  
  2.     var returnValue = false;  
  3.     var pos = container.indexOf(value);  
  4.     if (pos >= 0) {  
  5.         returnValue = true;  
  6.     }  
  7.     return returnValue;  
  8. }  
  1. includes([1,2,3],2) //=> true  
  2. includes([1,2,3],'a'//=> false  
Also tryed
  1. if (!String.prototype.includes) {//To check browser supports or not  
  2.         String.prototype.includes = function (str) {//If not supported, then define the method  
  3.             return this.indexOf(str) !== -1;  
  4.         }  
  5.     }  
but not work well for IE8.
 
Will anyone please help me.

Answers (1)