Semicolon(;) in Ecmascript 6 is no longer required?

Asked

Viewed 331 times

7

When using Ecmascript 6 (ES6) it is no longer necessary to use semicolons (;) at the end of each code line?

  • 3

    I understand that it is duplicated for two reasons: as confirmed by the answers given, the behavior of version 6 has not changed the situation, and there is extensively explained not only whether there is obligation or not, but also the various situations that can unfold from the lack of use.

2 answers

10


The point and comma (semicolon) has never been mandatory at the end of lines, its use is another, it aims to separate expressions who are on the same line or within a for.

Note that in the Javascript line breaks also have separating effect, see:

var i = 0; i++ // ponto e virgula é obrigatório

var i = 0      // ponto e virgula é opcional pois estão em linhas diferentes
    i++

Note ¹: the for has 3 expressions as explained by @Sergio and therefore needs both ;:

for ([inicialização]; [condição]; [expressão ao final de cada loop])

the for just doesn’t use the ; when you use the in (unless you wanted to add expressions for additional operations):

for ([variável] in [objeto que terá as propriedades iteradas])

or of:

for ([variável] of [objeto que terá os valores iterados])

Note ²: This occurs in other languages as well

Sources:

  • 1

    within the for does not work without the point and comma...

  • @Jedaiasrodrigues depends, be for a for() with in is without point and comma, if it is the for normal has ;. Or are you trying to say that my text is hinting at something wrong?

  • @Guilhermenascimento Or for... of is also without a semicolon. And, as stated in the comments in the answer below, when ; unbounded, ; is a statement. For example: umaLabel: ;, ;;;;;;;;;;;;;;;; ; ; ;, for (const{}of[]) ;, this is how most interpreters interpret: http://esprima.org/demo/parse.html?code=%3B

  • 1

    @Phanpy or I am not understanding, or have some error in my text, delimits as far as I know is within the context "limit", I said separate, what in my view is different. statement means "statement", which does not seem to me very far from expression (term I used in the reply). But if I have understood something wrong please guide me. Thank you

  • @Guilhermenascimento Yes, correct! I think I ended up mixing terms between the two answers :/

  • 1

    @Phanpy thanks for the tip from for ... of, I’ll edit and add ;)

Show 1 more comment

8

It’s not about ES6 use or not point and comma or not, is "a matter of religion" :)
That is: It is a matter of code style.

The ; is a symbol with Javascript functionality, to separate statements, in some cases one cannot ignore, for example: for (var i = 0; i < 10; i++), but at the end of the line is optional as long as there is a line break.

  • In fact, ; is also a statement, for example: label: ; for (;;) ;. Both good answers

  • @Phanpy the ; is not a statement, but delimits statements, even empty statements. That is to say, it is only a statement after ; have been used, or break line in the absence of ;. Maybe that’s what you meant too :)

  • Yes, ; delimits statements. When it delimits statements, it does not become a statement, but becomes when it does not delimit: http://esprima.org/demo/parse.html?code=%3B . And you can see that Abels require a statement at the front, so label: ; this code is valid. Ah! And blocks are not delimited by ;: http://esprima.org/demo/parse.html?code=%7B%7D%3B . When you see the link, you will see that Esprima will show a EmptyStatement on the tree!

  • @Phanpy think this is bug :D I realize that in practice it is like this, but I prefer to separate and say that a ; confirms the Empty statement, and does not create.

  • @Phanpy did not know this parser, good tip!. Take a look at this if you do not know: https://astexplorer.net/ is excellent and you can make codemods.

  • It looks totally like Esprima (he does the same with the ;) ! Now just need to fix an error of reserved names escaped with u (which has already been fixed in Esprima, but the version of the demos has not been updated yet) :-)

  • 1

    @Phanpy sends them a PR, it’s Facebook guys who did, are good contacts :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.