Adam Lindberg
Adam Lindberg

Reputation: 16587

Why are not top level anonymous functions allowed in JavaScript?

For example, in test.js this is allowed (and extremely pointless):

1 + 2;
(function () {});

When executing with node:

$ node test.js
$

The form function () {} (without parentheses) is allowed in other contexts, for example as an argument to another function.

Why is not the following allowed in the top level of a JavaScript file?

function () {};

Upvotes: 0

Views: 592

Answers (1)

SLaks
SLaks

Reputation: 887767

A statement that starts with function is a function statement.
Function statements must be named. (since they create a declaration in that scope)

Upvotes: 4

Related Questions