2

I know there is already a similar question as below, but I still have a question about ES6.

Why is a semicolon needed here?

As you see the website as below,

http://es6-features.org/#ArrayMatching

This code should work, but it causes that a and b are all undefined.

var list = [ 1, 2, 3 ]
var [ a, , b ] = list
[ b, a ] = [ a, b ]

The only way I found to fix it was by putting a semicolon on line 2

var list = [ 1, 2, 3 ]
var [ a, , b ] = list;
[ b, a ] = [ a, b ]

Isn't ES6 supposed to work without any semicolon? (I haven't used any semicolon in my javascript code so far, but now I am kind of lost. Should I just put a semicolon only when it is needed, or put a semicolon on every statement?)

Are there any other cases that I have to put a semicolon?

Jason KIM
  • 117
  • 7
  • 2
    You put it on two lines, but the code meant: `var [ a, , b ] = list[ b, a ] = [ a, b ]` (where the `[...]` you tried to use to destructure was treated as a square bracket property accessor against the previous variable) – Quentin Aug 24 '20 at 08:40
  • Thanks @Quentin. I know what you meant, then did the website I mentioned give us the wrong example? As far as I know, destructuring with [] is also one of the official ways of destructuring. Do you think destructuring with [] is the wrong syntax? – Jason KIM Aug 24 '20 at 08:46
  • 2
    "then did the website I mentioned give us the wrong example" — Because they made a mistake! – Quentin Aug 24 '20 at 08:49
  • 1
    "Do you think destructuring with [] is the wrong syntax?" — No, of course not. – Quentin Aug 24 '20 at 08:50
  • 2
    They have a toggle above the code block `syntactic sugar: reduced | traditional` which just adds and removes the seimicolon. It defaults to `reduced` which is wrong here. You can create an issue in the linked github – adiga Aug 24 '20 at 08:53
  • what's perplexing here is that `list[3, 1]` is a completely valid expression – marzelin Aug 24 '20 at 09:08
  • ES6 did not change any semicolon rules. I think you meant **Isn't ES1 (shipped with Netscape Navigator 4) supposed to work without any semicolon?** - yes, yes it did in 1994 and it still does now. But you have to follow the rules of the standard.js style guide. If you don't want to follow the rules of standard.js you **should** use semicolons everywhere – slebetman Aug 24 '20 at 10:32
  • 1
    @marzelin `list[3, 1]` is same as `list[1]` because of [the comma operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator) – adiga Aug 24 '20 at 10:45
  • Thanks @slebetman. Then, for you to use ES6, do you use semicolons everywhere, or just use it only with this case, or haven't you get any chance to face this issue so that you didn't need to put any semicolon? Actually, that is what I am really curious about because as far as I know, many developers don't use semicolon at all. – Jason KIM Aug 24 '20 at 11:47
  • I personally use semicolons everywhere but at this point that's a matter of taste. I used to advocate to always use semicolons and never leave them out because the rules for automatic semicolon injection are too complicated for humans (me) to remember. Then the standardjs style guide came out and people started following it. Then people who don't know about standardjs try to leave out semicolons without bothering to read the rules and you get this kind of bugs. So either always use semicolons or follow standardjs: https://standardjs.com/ – slebetman Aug 25 '20 at 01:06

0 Answers0