Points and commas in JS, Recommended at performance level?

15

I know that the use of; in JS, but for the JS interpreter that is more efficient, what do we say or not?

    
asked by Johnny Pachecp 15.03.2018 в 14:35
source

2 answers

26

If you omit the semicolon, the javascript interpreter does its best to infer where one block ends and the next begins.

There are border cases. The interpreter can assume that a line break implies a semicolon where it does not correspond. 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 interpreter's fault

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 executing the function immediately by passing the parameter console.log(mayuscula('hola')) to mayuscula . And that's going to throw you an error. I repeat, this is not an interpreter error. There are cases where you really want to execute the function immediately.

Another example. You declare a text string, a numeric variable and then execute a regular expression on the text 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 numero is 0 / (cadena/g.exec(texto)) that is, use the start of the regular expression as the division operator, and it will throw:

Uncaught ReferenceError: cadena is not defined
    
answered by 15.03.2018 / 15:18
source
12

Short Answer: Yes.

Long Response

To know if we have a good performance with this, we must check that it says the documentation of EcmaScript about the ycoma point

  

The sentences and statements of ECMAScript must end with a   semicolon. Such periods and commas can always appear   explicitly in the source text. For convenience, however,   such semicolons can be omitted from the source text in certain   situations. These situations are described by saying that the points and   commas are automatically inserted into the code token sequence   source in those situations.

Likewise, if you do not want to use semicolons for some reasons, you must respect the rules that you will find in the manual link as 11.9.1Rules of Automatic Semicolon Insertion ( Rules for automatic semicolon insertion) and if you want to see examples 11.9.2Examples of Automatic Semicolon Insertion (Automatic semicolon examples)

The ; means the end of an instruction and / or statement, but if we want to make a file .js that weighs less ( min.js ) it can that the ; that you did not add give you problems

let a="SoEs";console.log(a);

  let a="SoEs" console.log(a)

As we can see in some of the cases is necessary the semicolon and, this makes it difficult for the interpreter of javascript to assume things, the only fact that let the interpreter deduce when using semicolons and when not, it loses time interpret.

No Obstante,

In Some Frameworks they tell us that it's "Good Practices" Do not use it, but, Why?

to avoid "silly errors" that prevent the frameworks from compiling 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 bring it to. min.js in addition to other things, the most popular and used is Babel

    
answered by 15.03.2018 в 17:55