Headshot of Marius Schulz
Marius Schulz Front End Engineer

Semicolons after Function Declarations in JavaScript

This post takes a quick look at semicolons that follow a function declaration in JavaScript. I've seen these a bunch of times lately, and although they're not grammatically incorrect in this place, they're neither required nor of any use.

Note that all of the following applies to function declarations (which are statements), not function expressions (which are, well, expressions). Check out my blog post about the various kinds of function definitions in JavaScript if you're unsure about the difference.

Here's a simple function declaration whose closing brace is immediately followed by a semicolon:

function identity(x) {
  return x;

According to the language grammar, function declarations are statements that are not terminated by a semicolon. They're written without one, like this:

function identity(x) {
  return x;

In case there is a semicolon after the function declaration, the semicolon is parsed as a separate empty statement, which doesn't do anything. The upside is that empty statements following function declarations do no harm; the downside is that they don't provide any value whatsoever (here).

If you're interested in the language grammar, the syntax and semantics of the EmptyStatement are described in Section 13.4 of the specification.

You can easily verify how the program is parsed by running the code through a JavaScript parser such as Esprima. Entering the identity function into Esprima's online parser yields the following syntax tree:

Syntax Tree of a Function Declaration with a Trailing Semicolon

As you can see, the body of the program consists of two statements: a FunctionDeclaration and an EmptyStatement. This proves that the semicolon is not part of the function declaration, but a statement on its own.

If you're interested in the entire syntax tree, here you go:

Syntax Tree of a Function Declaration with a Trailing Semicolon

The morale of the story: If you're writing a function declaration, leave out the trailing semicolon!