Semicolon usage in JavaScript

David Y.

The problem

Are semicolons required in JavaScript? How should I use them?

The solution

In JavaScript, statements are separated by semicolons. Statements include variable assignments and function calls, as well as control flows (e.g. if ... else) and iterations (e.g. for). The code below provides some example usage:

const name = "John Doe"; const age = 35; function oldEnoughToBePresident(name, age) { if (age >= 35) { console.log(`${name} is old enough to run for president of the United States.`); return true; } else { console.log(`${name} is too young run for president of the United States.`); return false; } } oldEnoughToBePresident(name, age);

Note that while semicolons are used to separate statements inside the blocks provided to the if and else clauses of our if ... else statement, we do not place them after the if condition or the else itself. Doing this would cause JavaScript to interpret the if statement and our block as two separate statements, as below:

if (age >= 35); // if age >= 35, execute an empty statement (i.e. do nothing) { // this block is seen as separate from the if and will run regardless of the value of age console.log(name + " is old enough to run for president of the United States."); return true; }

An interesting feature of JavaScript is that its semicolons do not always need to be provided by the programmer – the JavaScript interpreter has a process called automatic semicolon insertion (ASI) which it uses to insert semicolons where needed, according to other heuristics such as line endings.

As a result, different JavaScript developers, teams and projects have different conventions about semicolon usage – some explicitly include them at the end of all statements, whereas others only include them in places where ASI does not act correctly.

For most code, ASI will behave in predictable ways, usually adding semicolons at the end of lines.

const name = "John Doe" // ; inserted here by ASI const age = 35 // ; inserted here by ASI if (age >= 35) { console.log(`${name} is old enough to run for president of the United States.`) // ; inserted here by ASI }

However, ASI considers only syntactical correctness, and may thus produce the wrong results in ambiguous situations. For example, lines starting with () will be interpreted as arguments for a function called on the previous line:

const age = 35 (age).toString() //; inserted here by ASI // the code becomes: const age = 35(age).toString(); // will invoke an error as 35 is not a function.

Similarly, lines starting with [] will be interpreted as accessing members of a variable in the previous line.

const name = "John Doe" [1, 2, 3].forEach(console.log) //; inserted here by ASI // the code becomes: const name = "John Doe"[1, 2, 3].forEach(console.log); // will throw an error

The MDN web docs provide a comprehensive overview of ASI’s behavior. Developers wishing to avoid semicolons should keep these rules in mind.

Get Started With Sentry

Get actionable, code-level insights to resolve JavaScript performance bottlenecks and errors.

  1. Create a free Sentry account

  2. Create a JavaScript project and note your DSN

  3. Grab the Sentry JavaScript SDK

<script src="https://browser.sentry-cdn.com/7.112.2/bundle.min.js"></script>
  1. Configure your DSN
Sentry.init({ dsn: 'https://<key>@sentry.io/<project>' });

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.