Whether or not to use semicolons at the end of lines in JavaScript

February 24, 2022

Whether or not to use semicolons at the end of lines in JavaScript

In this article we will learn on whether or not it is necessary to use semicolons at the end of lines – and this in JavaScript, where practice can be optional

semicolons at the end of lines in JavaScript cover

Hey you programmer, ok? Let’s learn more about JavaScript and the semicolon!

Although the language allows instructions without the semicolon (;), it is recommended to use it on each line

Because when there is no use, you open a possibility of problems happening during the execution of your code

The semicolon is the way you have to delimit that a statement has been terminated for JavaScript

One of the problems – compression

If the code is compressed with the intention of generating more speed in its execution

The lack of ; may generate a syntax error

See the example:


In compression the code looks like this:


This will generate the error:

Uncaught SyntaxError: Unexpected identifier

So with the declarations with the ; – the error would be prevented as the separation of instructions would be clear to JS

Expression Problems

Mathematical expressions can also generate errors if the semicolon is not used correctly.

let a = 0
let b = 5
let c = 5
let d = 1
let e = 2
a = b + c
(d + e).print()

This will generate an error:

Uncaught TypeError: c is not a function

Because JS will interpret the expression as follows:

a = b + c(d + e).print();

See then that the lack of ; may represent some unwanted errors, so it is better to put it

Of course, these are more specific situations, as there is an auto insert of ;

But this auto insert fails in some instructions, like the one declared above

And another important point is that two declarations on the same line will need a ; between them

As is the case expressed in code compression


In this article we learned the differences between using or not using semicolons at the end of lines in JavaScript

It is optional in the language, but by the examples presented in this article we see that some problems can arise, so to guarantee 100% of the cases use the semicolon

Remember that the language has an auto inserter, so the ; becomes optional when building a program

Want to learn more about JavaScript? Click here!

Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x