The Great JavaScript Debate: To Semicolon or Not?
General Javascript

The Great JavaScript Debate: To Semicolon or Not?

3 min read

Since I’ve started learning this language, JavaScript has undergone some heavy changes. Most notably, it seems to be the norm to not use semicolons anymore. Popular projects, such as Vue, seem not to use them at all.

People argue that automatic semicolon insertion (ASI) is enough. We shouldn’t type more than we have to.

Are semicolons actually important, though?

A Matter of Opinion?

You might just think semicolon usage comes down to opinion-it’s up to the developer to use them or not, depending on what they prefer. However, a question arises. Are they actually required, under any circumstances?

The answer is yes. Semicolons are indeed required under certain circumstances, and the lack of a semicolon can even cause bugs in your code.

Confused Programmer

Let’s take a look at some examples of when a semicolon is absolutely necessary in JavaScript.

Multiple Statements on a Single Line

Now, I don’t know why anyone would ever want to write code like this, but take a look below:

let x = 5; let y = 10; console.log(x + y);

If you were to remove the semicolons, and paste the code into the console, you’d get a syntax error: unexpected token: 'let'. Now, maybe you just want to write a quick script to do some trickery on a webpage. In that case, you’ll need to use the semicolons.

Immediately Invoked Function Expressions

Semicolons are especially important when you begin a new line with an open parenthesis ( or an open square bracket [. These characters can be misinterpreted by the JavaScript engine when semicolons are left out, as JavaScript might assume you’re attempting to immediately invoke a function or access an array.

let x = 5
(function() {
  console.log('Hello, world!');
})(); // Uncaught TypeError: 5 is not a function

This code looks innocent, but JavaScript will try to interpret the second line as if it’s part of the first one, which makes no sense, and will indeed throw an error.

Of course, adding a semicolon at the end of the first statement fixes the issue.

Consistency Across Languages

If you’re a developer who works with multiple programming languages, using semicolons can help you maintain a consistent coding style. In many other languages, such as Java, C#, and C++, semicolons are mandatory.

Switching back and forth between languages without semicolons in one can sometimes lead to simple errors (and massive frustration) when you switch to another language where they are required.

To Use, or Not to Use Semicolons?

JavaScript allows omitting semicolons in many situations, but I’d argue that for clarity, maintainability, and to avoid headaches (and insanity), it’s good practice to just use semicolons in your code. It also keeps you consistent across different languages. So, seriously… why not? 😅

About the Author

Matt Fay

Matt Fay

Matt is the founder of JavaScript Today, a platform dedicated to high-quality JavaScript education and commentary. With a deep curiosity for technology, he gravitates toward understanding how things work and has been diving into the world of information security. Outside of coding, Matt enjoys exploring languages; he can understand some Russian and Italian, and is currently learning Thai.

Learn AI Skills with Codecademy (729x90)

Discussion (Loading...)

Join the conversation

Join the Discussion

Sign in to share your thoughts and engage with the JavaScript Today community.

Loading comments...

Related Articles

Continue your learning journey with these related posts

Getting Started with Google Cloud
General

Getting Started with Google Cloud

Discover the power of Google Cloud. Explore its key services, benefits for businesses, and how it compares to AWS and Azure. Get started with cloud computing.

Matt Fay Matt Fay
1 min December 7, 2024

Never Miss a JavaScript Update

Join our community of developers and receive insightful articles and coding tips every week.