-2

What's so terribly wrong about the following code

var obj = {a:1}
[1,2,3].forEach(console.log)

that Babel produces this malfunctional concoction?

var obj = { a: 1 }[(1, 2, 3)].forEach(console.log);

for any of ES201[567] preset. Verified with https://babeljs.io/repl.

Yes, terminating the first line with semicolon helps. Is it still a dangerous practice to not use semicolons?

UPDATE: before downvoting on the grounds of "gosh of course you should use semicolons", please acknowledge the evolution of not using them, along with arrow functions and ever growing toybox of array/object prototype functions. All modern JS examples do that, and ES6 newbie gets confused easily.

Pavel Zdenek
  • 6,883
  • 1
  • 18
  • 37
  • 1
    semicolons do matter in js – Davin Tryon Jan 02 '17 at 10:22
  • 1
    Use semicolon, after the first line? – kind user Jan 02 '17 at 10:22
  • 2
    Babel doesn’t misinterpret the code. Put that code into the console. It doesn’t do what you think it does. Yes, _use semicolons_. – Sebastian Simon Jan 02 '17 at 10:23
  • 1
    `(1, 2, 3)` returns 3, unless it is part of a function call. read more: [comma operator](http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator) – Nina Scholz Jan 02 '17 at 10:31
  • It's best practice to add a semicolon. If obj was followed by a function or something else, you wouldn't have known. So, i guess its a good thing you encountered this. – Ademola Adegbuyi Jan 02 '17 at 10:35
  • ASI has nothing to do with arrow functions, nor does the "growing toybox of array/object prototype functions". What do you mean by that? *Is it still a dangerous practice to not use semicolons?* It's not **still** a dangerous practice; it has **always** been one. –  Jan 02 '17 at 12:21

1 Answers1

1

The problem is eluded to in the code compiled by Babel: the two lines you have given it are interpreted as one.

Is it still a dangerous practice to not use semicolons?

In my opinion, no. White-space is collapsed in JavaScript meaning multiple lines can be interpreted as one, so there are a few cases in which they are necessary, but whether this means that using semi-colons is an absolute must is really down to personal preference. Some people feel very strongly that you should, and they may well be right...

Anyhow, as you discovered, one of these cases is ending a line with an object or array and starting the next with an array literal ([]). You can read about the remaining cases here.

If the code you are working on does not use semi-colons, for example if it uses the standard code style, you can..

..add a semi-colon to the end of the first line:

var obj = {a:1};
[1,2,3].forEach(console.log)

..add a semi-colon to the beginning of the second line:

var obj = {a:1}
;[1,2,3].forEach(console.log) // beginning of last line

..avoid having to use a semi-colon at all by assigning the array literal to a variable:

var obj = {a:1}
var arr = [1,2,3]
arr.forEach(console.log)

____

Side note...

The reason the contents of your array literal are wrapped in brackets is because Babel has interpreted your array literal as an attempt to access a property on the object literal defined in the previous line. Of course, you can only access one property at a time using the array[index] syntax, so Babel uses the comma operator to take a single value from (1, 2, 3), which will be 3.

Read about the comma operator on MDN.

sdgluck
  • 18,456
  • 4
  • 56
  • 83
  • Thanks. [This](https://github.com/feross/standard/blob/master/RULES.md#semicolons) is the most enlightening resource, which by the way also explains why i'm seeing a semicolon in front of IIFE on the internets, even if the source in question uses semicolons generally. – Pavel Zdenek Jan 02 '17 at 12:02
  • Confused, you say "no, it is (not) still a dangerous practice not to use semi-colons", then spend the rest of your post talking about all the dangers involved. Your second example of putting the semicolon at the beginning of the line is just ridiculous; why not just put it at the end of the preceding line where it belongs? –  Jan 02 '17 at 12:18
  • @torazaburo Well I was speaking more to the idea of not using semi-colons at all. I don't think that this is dangerous, however there are some things to _keep in mind_. Those aren't dangers, just caveats. Whether it is ridiculous to put a semi-colon at the beginning of line, I don't really care. I see it done all the time. I did include an example that demonstrates your preference. – sdgluck Jan 02 '17 at 12:24
  • If my code doesn't work, and I have to screw around to figure out why, or examine Babel output, or post to SO to get help, then the distinction between "danger" and "caveat" suddenly seems quite academic. We should really try to stamp out the moronic no-semicolon approach. As you probably know, well over 90% of all JS projects do use semi-colons. There's no reason not to. Even Brendan Eich has stated that ASI was a horrible mistake. Even the crotchety old Douglas Crockford uses semi-colons. –  Jan 02 '17 at 12:30
  • @torazaburo To the best of my ability I answered the question asked. If you disagree, post an alternative. Without wanting to offend, I'm just not interested in engaging with you on this trivial matter. – sdgluck Jan 02 '17 at 12:37