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
Hacktoberfest 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 moreCreating a Real Time Chat Application with React, Node, and TailwindCSS
In this tutorial, we will show you how to build a real-time chat application using React and Vite,as well as a simple Node backend.
Read moreThe Importance of Staying Active as a Software Developer
In today’s fast-paced digital world, developers often find themselves glued to their screens for extended periods. While this dedication is commendable, it comes with its own set of challenges.
Read more