All the good programmers I know use it. Because it’s ambiguous not to use it in some situations. And if you need to use it on some it’s weird not to use it on others. And you start to create a dialect of the language between those who use it and those who don’t.
There is no gain in not using. In general it is adopted by those who think that tapping the finger on a key gives a gain, but leaves the doubt whether it really ended the statement there, because there are cases that he can continue in another line. Makes it difficult to read all in all cases to save effort of a typed key.
Some say that without it it saves a byte to transmit. I find a gain too small to compensate for the problem. In general people spend a lot more on other things, and if you’re going to use that argument, all code should be written in a very unreadable way to save bytes. The economy should come more than one compression (static file should always be compressed) and minification (good mini-finishers should take the comma where gives and leave where you need or may need, but many do not make it so complicated is parse JS code for allowing this kind of thing, some minifica less precisely by the lack of a semicolon, so it’s a very wrong reason). And today JS is very used in backend that it makes no sense to have that kind of concern. Again, it will create two dialects?
I’ve seen people say it’s easier to insert something in the middle, especially using method chaining, but it only shows that the motive is laziness, a wrong motive.
Javascript was created to have a syntax similar to C, some even think it bad, but it was thought so, so the ;
is important, even if the compiler lets, in some cases do not use. If you are used to continue using.
Turn and move someone comes up with a problem because they didn’t have the ;
and the behavior gets strange (some cases make mistakes, others only do what does not expect).
There are languages that have been able to make the use optional in an unambiguous way, that’s fine, but it’s not the case of JS.
In fact they put the ;
for you, JS also does this, but it has no rules as clear, and even differs from implementation to implementation. So don’t do this to yourself, even if it works most of the time. So I’m not going to put the rules like some people do because whoever creates a language compiler doesn’t understand the rules, they always post about simple rules and... wrong.
For the same reason I don’t like:
if (x == 1)
console.log("ok");
Or does:
if (x == 1) console.log("ok");
because there is a line, not confusing with a block, or does:
if (x == 1) {
console.log("ok");
}
This can cause problems when you put more than one line in the unclear condition of what the block is.
The problems you will have are not so relevant, but they exist. I think the biggest reason to always adopt the ;
is consistency and readability, but I will cite examples of problems.
A example of question that seemed here.
Example in Soen.
Example:
const hey = 'ola'
const you = 'ola'
const heyYou = hey + ' ' + you
['o', 'l', 'a'].forEach((letter) => console.log(letter))
Now writing right:
const hey = 'ola';
const you = 'ola';
const heyYou = hey + ' ' + you;
['o', 'l', 'a'].forEach((letter) => console.log(letter));
Other:
const x = 1
const y = 1
const z = x + y
(x + y).toString()
I’m not gonna post the right one at all, fix you up by putting the ;
.
var x = 1 + 1
-1 + 1 === 0 ? console.log("sim") : console.log("não")
I do not like some examples of the other answers (not all), for example I do not like the answer accepted because it is an example that is clearly mandatory, the problem is not to omit the ;
in the same line, but yes when it changes lines. Although at the time I had positive together with two others. The answer is good, well thought out, and correct, but I could have a better example.
Complement.
If you want to delve into the subject, I recommend two books: Javascript Patterns, by Stoyan Stefanov, and Maintainable Javascript, by Nicholas Zakas. The presence or absence of the semicolon can change the meaning of an instruction. Otherwise, it is good practice to use it because it makes the code more readable.
– Oralista de Sistemas