If you omit the semicolon, the javascript interpreter does its best to infer where one block ends and the next begins.
There are edge cases. The interpreter may assume that a line break implies a semicolon where it does not belong. For example if you write
function escribe5() {
return
5
}
console.log(escribe5())
the interpreter takes it as
function escribe5() {
return;
5;
}
And therefore returns undefined.
Another case that is not the fault of the interpreter
Think you declare a function expression
var mayuscula=function(texto) {
return texto.toUpperCase();
};
(console.log(mayuscula('hola')));
logically that returns HOLA.
If you omit the semicolon:
var mayuscula=function(texto) {
return texto.toUpperCase()
}
(console.log(mayuscula('hola')))
The interpreter assumes that you are immediately executing the function by passing it the console.log(mayuscula('hola'))a parameter mayuscula. And that will throw you an error. I repeat, this is not an interpreter error. There are cases where you really want to run the function immediately.
Another example. You declare a string, a numeric variable, and then run a regular expression on the string:
var texto,numero;
texto="cadena de texto";
numero=0;
/cadena/g.exec(texto);
This prints (in the browser console)
["cadena", index: 0, input: "cadena de texto", groups: undefined]
If you do it without a semicolon:
var texto,numero
texto="cadena de texto"
numero=0
/cadena/g.exec(texto)
The interpreter will assume that numerois 0 / (cadena/g.exec(texto)), use the start of the regular expression as the division operator, and return:
ECMAScript statements and declarations must end with a semicolon. Such semicolons can always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token sequence in those situations.
In the same way, if you don't want to use a semicolon for some reasons, you must respect the rules that you will find in the manual link as 11.9.1Rules of Automatic Semicolon Insertion and if you want to see the examples 11.9. 2Examples of Automatic Semicolon Insertion
The ;means the end of an instruction and/or Statement , but if we want to make a file .jsthat weighs less ( min.js) the ;ones you did not add may give you problems
let a="SoEs";console.log(a);
let a="SoEs" console.log(a)
As we can see in some of the cases the semicolon is necessary, and this makes it difficult for the javascript interpreter to assume things, the mere fact that letting the interpreter deduce when to use a semicolon and when not, wastes the interpreter's time.
Nevertheless,
Some Frameworks tell us that it is "Good Practice" not to use it, but why?
to avoid "stupid errors" that prevent frameworks from being able to compile quickly, avoiding the bad time of the semicolon.
And what does the framework do when I want to publish a JavaScript application?
There is a program that is responsible for transpiling the code, placing ";" and take it to . min.jsbesides other things, the most popular and used is Babel
If you omit the semicolon, the javascript interpreter does its best to infer where one block ends and the next begins.
There are edge cases. The interpreter may assume that a line break implies a semicolon where it does not belong. For example if you write
the interpreter takes it as
And therefore returns
undefined
.Another case that is not the fault of the interpreter
Think you declare a function expression
logically that returns
HOLA
.If you omit the semicolon:
The interpreter assumes that you are immediately executing the function by passing it the
console.log(mayuscula('hola'))
a parametermayuscula
. And that will throw you an error. I repeat, this is not an interpreter error. There are cases where you really want to run the function immediately.Another example. You declare a string, a numeric variable, and then run a regular expression on the string:
This prints (in the browser console)
If you do it without a semicolon:
The interpreter will assume that
numero
is0 / (cadena/g.exec(texto))
, use the start of the regular expression as the division operator, and return:Short Answer: Yes.
Long Answer
To know if we have a good Performance with this, we must validate what the EcmaScript documentation says about semicolons
In the same way, if you don't want to use a semicolon for some reasons, you must respect the rules that you will find in the manual link as 11.9.1Rules of Automatic Semicolon Insertion and if you want to see the examples 11.9. 2Examples of Automatic Semicolon Insertion
The
;
means the end of an instruction and/or Statement , but if we want to make a file.js
that weighs less (min.js
) the;
ones you did not add may give you problemsAs we can see in some of the cases the semicolon is necessary, and this makes it difficult for the javascript interpreter to assume things, the mere fact that letting the interpreter deduce when to use a semicolon and when not, wastes the interpreter's time.
Nevertheless,
Some Frameworks tell us that it is "Good Practice" not to use it, but why?
to avoid "stupid errors" that prevent frameworks from being able to compile quickly, avoiding the bad time of the semicolon.
And what does the framework do when I want to publish a JavaScript application?
There is a program that is responsible for transpiling the code, placing ";" and take it to .
min.js
besides other things, the most popular and used is Babel