Search code examples
javascriptinternet-explorer-11eval

Javascript Error: SyntaxError: Syntax Error in eval() function


I've got this piece of string below stored in a variable javascript_string_above:

function run(hideBtn, list) {
  if(hideBtn) {
    return list.filter(function(x) { return x.period == 1; });
  }
  else {
    return list;
  }
}

run(hideBtn, list);

The string above is passed to eval function like the following:

const hideBtn = true;
const list = this.list; //assuming this list has values in it.
const resultList = eval(javascript_string_above);

After eval, I'm geting a syntax error in the console log. This is the error:

Error: Uncaught (in promise): SyntaxError: Syntax error
SyntaxError: Syntax error
   at PaymentScheduleComponent.prototype.setRecord (eval code:136:17)
   at Anonymous function (eval code:85:17)
   at SafeSubscriber.prototype.__tryOrUnsub (eval code:238:13)
   at SafeSubscriber.prototype.next (eval code:185:17)
   at Subscriber.prototype._next (eval code:125:9)
   at Subscriber.prototype.next (eval code:89:13)
   at FilterSubscriber.prototype._next (eval code:89:13)
   at Subscriber.prototype.next (eval code:89:13)
   at TakeWhileSubscriber.prototype.nextOrComplete (eval code:84:13)
   at TakeWhileSubscriber.prototype._next (eval code:79:9)
   {
      [functions]: ,
      __proto__: { },
      description: "Uncaught (in promise): SyntaxError: Syntax error
SyntaxError: Syntax error
   at PaymentScheduleComponent.prototype.setRecord (eval code:136:17)
   at Anonymous function (eval code:85:17)
   at SafeSubscriber.prototype.__tryOrUnsub (eval code:238:13)
   at SafeSubscriber.prototype.next (eval code:185:17)
   at Subscriber.prototype._next (eval code:125:9)
   at Subscriber.prototype.next (eval code:89:13)
   at FilterSubscriber.prototype._next (eval code:89:13)
   at Subscriber.prototype.next (eval code:89:13)
   at TakeWhileSubscriber.prototype.nextOrComplete (eval code:84:13)
   at TakeWhileSubscriber.prototype._next (eval code:79:9)",
      message: "Uncaught (in promise): SyntaxError: Syntax error
SyntaxError: Syntax error
   at PaymentScheduleComponent.prototype.setRecord (eval code:136:17)
   at Anonymous function (eval code:85:17)
   at SafeSubscriber.prototype.__tryOrUnsub (eval code:238:13)
   at SafeSubscriber.prototype.next (eval code:185:17)
   at Subscriber.prototype._next (eval code:125:9)
   at Subscriber.prototype.next (eval code:89:13)
   at FilterSubscriber.prototype._next (eval code:89:13)
   at Subscriber.prototype.next (eval code:89:13)
   at TakeWhileSubscriber.prototype.nextOrComplete (eval code:84:13)
   at TakeWhileSubscriber.prototype._next (eval code:79:9)",
      name: "Error",
      promise: { },
      rejection: { },
      stack: "Error: Uncaught (in promise): SyntaxError: Syntax error
SyntaxError: Syntax error
   at PaymentScheduleComponent.prototype.setRecord (eval code:136:17)
   at Anonymous function (eval code:85:17)
   at SafeSubscriber.prototype.__tryOrUnsub (eval code:238:13)
   at SafeSubscriber.prototype.next (eval code:185:17)
   at Subscriber.prototype._next (eval code:125:9)
   at Subscriber.prototype.next (eval code:89:13)
   at FilterSubscriber.prototype._next (eval code:89:13)
   at Subscriber.prototype.next (eval code:89:13)
   at TakeWhileSubscriber.prototype.nextOrComplete (eval code:84:13)
   at TakeWhileSubscriber.prototype._next (eval code:79:9)
   at resolvePromise (eval code:824:25)
   at ZoneAwarePromise (eval code:893:17)
   at Anonymous function (eval code:38:5)
   at Anonymous function (eval code:176:43)
   at SafeSubscriber.prototype.__tryOrUnsub (eval code:238:13)
   at SafeSubscriber.prototype.next (eval code:185:17)
   at Subscriber.prototype._next (eval code:125:9)
   at Subscriber.prototype.next (eval ",
      Symbol(observable)_h.143sf9as4rn: undefined,
      Symbol(rxSubscriber)_g.143sf9as4rn: undefined,
      task: { },
      zone: { }
   }

Please help, I've been having this issue for so long. This is only encountered in IE11


Solution

  • I found the root cause. The function inside the filter() method is translated into lambada expression:

    FROM

    list.filter(function(x) { return x.period == 1; });

    TO

    list.filter(x => x.period == 1);

    IE11 does not support arrow functions "=>" thus throwing a syntax error.