Sentry Answers>JavaScript>

Semicolon usage in JavaScript

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:

Click to Copy
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:

Click to Copy
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.

Click to Copy
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:

Click to Copy
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.

Click to Copy
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.

  • ResourcesImprove Web Browser Performance - Find the JavaScript code causing slowdowns
  • SentryJavascript Error Monitoring & Tracing
  • ResourcesJavaScript Frontend Error Monitoring 101
  • Syntax.fmListen to the Syntax Podcast
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,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.

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