David Y.
—Are semicolons required in JavaScript? How should I use them?
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.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.