0

I am using Array.from() function which is supported in all browsers, except IE:

function range(start, end) {
    return Array.from(Array(end - start + 1), (_, i) => i + start);
}

Instead of Array.from() what function can I use to make my code compatible with IE?

double-beep
  • 5,031
  • 17
  • 33
  • 41

3 Answers3

4

Use a Polyfill.

if (!Array.from) {
    Array.from = (function () {
        // The code gose here...
    })();
}

As described in Mozilla please refer below link,

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Polyfill

0xdw
  • 3,755
  • 2
  • 25
  • 40
  • Syntax error is coming near by Array.from line on IE function range(start, end) { return Array.from(Array(end - start + 1), (_, i) => i + start); } I am using below Polyfill in my js file but still syntax error is coming – Pooja Patwa Jul 16 '19 at 07:07
0

Use a simple for loop

function range(start, end) {
    var array = [];
    for(i = start; i<=end; i++) {
      array.push(i);
    }
    return array;
}
console.log(range(3, 5));
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

The problem is not Array.from it is the arrow function, try

function range(start, end) {
  return Array.from(Array(end - start + 1), function (_, i) { return i + start; });
}
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60