Do you recommend using semicolons after every statement in JavaScript?

Active3 hr before
Viewed126 times

9 Answers


The process that does this is called Automatic Semicolon Insertion.,This is all possible because JavaScript does not strictly require semicolons. When there is a place where a semicolon was needed, it adds it behind the scenes.,when the end of the source code file is reached,instead raises a TypeError: b is not a function exception, because JavaScript tries to interpret it as

const hey = 'hey'
const you = 'hey'
const heyYou = hey + ' ' + you

['h', 'e', 'y'].forEach((letter) => console.log(letter))
load more v

Yes, you should use semicolons after every statement in JavaScript., 1 For the sake of consistency and future readability, include semicolons after every statement. +1 – JustinT Jan 14 '09 at 18:30 ,I'd say consistency is more important than saving a few bytes. I always include semicolons.,Software Recommendations

An ambiguous case that breaks in the absence of a semicolon:

// define a function
var fn = function() {
} // semicolon missing at this line

// then execute some code inside a closure
(function() {

This will be interpreted as:

var fn = function() {
}(function() {
load more v

If you're going to write your JavaScript without optional semicolons, it's probably good to at least know what ASI is doing. For example, compression or minification could cause your valid code to throw an error because those programs may rely on semicolons. ,I program in C# for my job so am required to use semicolons at the end of the lines. But at home when I'm working on something in Lua, or Javascript, I skip the semicolons. They are generally NOT needed so I don't include them.,During one of our first JavaScript lectures at the Flatiron School, the instructor mentioned that semicolons are optional in JavaScript… except when they’re not 🤔 ,My thinking is still using semicolons for convention and readable code. I'm writing application using Vuetify, and default eslint setting without semicolons. That make me feel not comfortable with it.

var beef
var cheese
load more v

I put out a poll on Twitter to test the waters, and I found lots of semicolon supporters:,After using semicolons for years, in the fall of 2017 I decided to try avoiding them when I could. I set up Prettier to automatically remove semicolons from my code, unless there was a particular code construct that required them.,It’s important to know the rules that power semicolons. This will allow you to avoid writing code that will generate bugs before it does not behave like you expect.,Be careful — some people are very opinionated about semicolons. I don’t care, honestly. The tool gives us the option not to use it, so we can avoid semicolons if we want.

Take this:

const hey = 'hey'
const you = 'hey'
const heyYou = hey + ' ' + you['h', 'e', 'y'].forEach((letter) => console.log(letter))
load more v

The official guidelines say that JS uses a semi colon to terminate a statement.,The official guidelines,All of these statements can end with a semi colon, but none of them must:,The last quirk to note is that inside a for loop, you only need semi colons after the first two statements, but not the third:

All of these statements can end with a semi colon, but none of them must:

const i; // variable declarationi = 5;                        // value assignmenti = i + 1;                    // value assignmenti++;                          // same as abovevar x = 9;                    // declaration & assignmentvar fun = function() {...};   // var decl., assign., & func. defin.alert("hello, world!");       // function call
load more v

For better or worse, JavaScript was originally designed to include Automatic Semicolon Insertion (ASI) and we are stuck with it.,It wasn’t long before I learned about JavaScript’s Automatic Semicolon Insertion (ASI). Basically, JavaScript will put semicolons in for you automatically if you leave them out. So, they aren’t mandatory after all.,When I first started writing JavaScript I thought that semicolons were mandatory. I was learning about jQuery at the time and all of the documentation I was reading showed a semicolon at the end of every statement.,Situations like this don’t come up a lot — but they do sometimes and JavaScript developers should be prepared for them.

Puppeteer (Headless Chrome Node.js API) seemed to be a good way to do this, so I took it for a spin. The docs feature this snippet of code to get you started:

const puppeteer = require('puppeteer');
(async () => {
   const browser = await puppeteer.launch();
   const page = await browser.newPage();
   await page.goto('');
   await page.screenshot({
      path: 'example.png'
   await browser.close();
load more v

You shouldn’t put a semicolon after a closing curly bracket }. The only exceptions are assignment statements, such as var obj = {};, see above.,1. After a closing curly bracket,The semicolon is only obligatory when you have two or more statements on the same line:,When do you need a semicolon? Here's a handy cheat sheet!

The semicolon is only obligatory when you have two or more statements on the same line:

var i = 0;
i++ // <-- semicolon obligatory
//     (but optional before newline)
var i = 0 // <-- semicolon optional
i++ // <-- semicolon optional
load more v

Semicolons serve to separate statements from each other, and a FunctionDeclaration is not a statement.,FunctionDeclarations are evaluated before the code enters into execution, hoisting is a common word used to explain this behaviour.,I use them after function-as-variable declarations:,Semicolons after function declarations are not necessary.

I've seen different developers include semicolons after functions in javascript and some haven't. Which is best practice?

function weLikeSemiColons(arg) {
   // bunch of code


function unnecessary(arg) {
   // bunch of code
load more v

Use semicolons for readability but avoid these traps that stem from newline terminated statements.,This is going to be a plea for the opposite. Please do use the semicolon to end a statement even though it's not mandatory. It will give you plenty of benefits. They all have to do with code readability. They contribute to code maintainability, code quality, and the number of bugs lurking in your code.,In JavaScript, however, it's not mandatory to use semicolons. A newline character terminates statements according to specific rules. Isaac Schlueter has a good recap, along with few opinionated guidelines.,Using the semicolon as a statement ending character is one of the ways to help your eyes navigate in program code.

let counter = 0;
load more v

Other "using-undefined" queries related to "Do you recommend using semicolons after every statement in JavaScript?"