Always use ;
and this is because:
JavaScript is a language that makes statements to the browser. The character used to separate these statements is ;
and it should always be used.
The code can work without the semicolon after a statement, but for this there must be an end-of-line or a correct syntax.
Thank you to @Gabriel Gartz for alerting us to the fact that any logical operator, mathematical or scope delimitation allows to generate code execution equally.
The biggest problem comes from the fact that we all want to optimize the files downloaded to the browser, such as .js
and .css
files for which we apply compression (remove line breaks, comments, spaces and tabulations). In these cases, if there is no correct separation of declarations to the browser, they will be interconnected and syntax errors will appear.
Let's look at examples:
-
Example 01 (click to open JSFIddle)
Make an alert without the ;
:
alert("olá")
It runs all as expected, we received an alert.
-
Example 02 (click to open JSFIddle)
Make two alerts without the claims separator, but each alert on your line separated by "end-of-line":
( r
, n
or rn
depending on the operating system where the file was created)
alert("olá")
alert("olá novamente")
It runs all as expected, we received two alerts.
-
Example 03 (click to open JSFIddle)
Make two separate alerts with the ;
:
alert("olá"); alert("olá novamente")
It runs all as expected, we received two alerts.
-
Example 04 (click to open JSFIddle)
Make two alerts without separating statements ;
and with compressed code:
alert("olá")alert("olá novamente")
Nothing happens and we get a script error on the page:
SyntaxError: missing; before statement
alert ("hello") alert ("hello again")
-
Example 05 (click to open JSFIddle)
Based on the call made by @Gabriel Gartz, you can verify that if the syntax is correct, the code runs without errors, even if the statements are separated by something other than ;
:
alert('foo')&alert('bar')|alert('zaz')
It will generate three alerts as expected.
Automatic insertion of the semicolon ( ;
)
There are some declarations that automatically receive an insertion of ;
:
┌────────────┬────────────────────────────────────┐
│ DECLARAÇÃO │ EXEMPLO DA DECLARAÇÃO │
├────────────┼────────────────────────────────────┤
│ empty │ ; │
├────────────┼────────────────────────────────────┤
│ variable │ var a = 1; │
├────────────┼────────────────────────────────────┤
│ expression │ greeting = "Hello " + name; │
├────────────┼────────────────────────────────────┤
│ do-while │ do qualquerCoisa while (condição); │
├────────────┼────────────────────────────────────┤
│ continue │ continue; │
├────────────┼────────────────────────────────────┤
│ break │ break; │
├────────────┼────────────────────────────────────┤
│ return │ return; │
├────────────┼────────────────────────────────────┤
│ throw │ throw "Error2"; │
└────────────┴────────────────────────────────────┘
These are declarations that must be terminated with the declaration argument ;
, which is why they are automatically inserted if it is not found.
What are the rules for this to happen:
Code reading is performed from left to right. If an offensive symbol symbol is found that is not allowed in any grammar production, a semicolon is automatically inserted if one of the following conditions is true:
- The offending symbol is separated from the previous symbol in at least one "end-of-line";
- The token is
}
.
Notes:
Automatic insertion of statement separators is an extended topic subject to many rules, but explains why we can get the code to work without using the declarations tab when we have a "end-of-line" or a valid syntax.
In case of files already compressed to suppress unnecessary characters to the maximum, it is almost impossible to insert the declarations separator automatically, as one of the above conditions is no longer present.
Documentation: 7.9 Automatic Semicolon Insertion (English)