0

Have a look into the es6 code below:

let person = "John Malkovich"
(() => {
  console.log("Original person was ", person);

  person = "Drew Barrymore";

  console.log("New person " + person);
})();

When I execute this program it throws me this error:

"TypeError: \"John Malkovich\" is not a function

Now I know that if I add a ; at the end of line 1 i.e let person = "John Malkovich"; the program works. My only headache is not being able to understand why that weird error is occurring.

riazosama
  • 362
  • 2
  • 15

1 Answers1

1

If the syntax is valid up to this point, an opening parentheses following an expression (perhaps with a newline in between) will result in the interpreter trying to invoke the expression as a function. That is:

someExpression()

or

someExpression ()

or

someExpression
()

will all try to invoke someExpression. No semicolon is automatically inserted because if someExpression happens to be invokable, the syntax is valid. (ASI generally only inserts semicolons, to separate distinct statements from each other, when the syntax would be invalid otherwise)

CertainPerformance
  • 260,466
  • 31
  • 181
  • 209