0

The code below gives the following error: VM502:1 Uncaught SyntaxError: Function statements require a function name and I assume it doesn't work because, as per the error message, function declarations require a function name.

function (){
var a = 'dog';
console.log(a);
}

The second code below doesn't produce an error, but gives "dog", because it is not a function declaration but an immediately invoked function expression...

(function(){
var a ='dog';
console.log(a);
})()

Why don't functions inside iife require a name?

chu8
  • 105
  • 1
  • 1
  • 10
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Unnamed_function_statement – David784 Jul 15 '21 at 18:59
  • 1
    Because at top-level `function` starts a named function definition. – Barmar Jul 15 '21 at 18:59
  • 1
    "*Why don't functions inside iife require a name?*" the IIFE *is* the function "inside the IIFE" you mention. So it's a bit redundant and confusing to call it that. The reason why function expressions don't require a name is simple - they are function expressions and function expressions don't require a name. – VLAZ Jul 15 '21 at 19:07

0 Answers0