The Great JavaScript Debate: To Semicolon or Not?
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.
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? 😅
Related Posts
Finding Free and Discounted Programming Books
As an avid reader, I’m always looking for places to find my next book. If they’re free, even better. Although it’s not always so easy finding them, there are plenty available online.
Read moreGetting Started with Google Cloud
In this article, we’re going to be taking a first look at Google Cloud, a leading player in the world of cloud computing, offers services and tools designed to drive innovation and ease operations.
Read moreHacktoberfest 2024: Get a Free JavaScript Today Sticker
October is here, and that means one thing in the tech world—Hacktoberfest! This annual event, powered by DigitalOcean, Cloudflare, Quira, and other sponsors, encourages developers of all skill levels to contribute to open-source projects.
Read more