Use of semicolons in ES6

Javascriptnode.jsEcmascript 6

Javascript Problem Overview


I was under the impression semicolons became obsolete with ES6. However, I came across this today:

Doesn't work:

let i = 0

[0, 1, 2, 3, 4, 5, 6].forEach(item => console.log(item))

Works:

let i = 0;

[0, 1, 2, 3, 4, 5, 6].forEach(item => console.log(item))

Why is the semicolon necessary here, and when should I use them?

Javascript Solutions


Solution 1 - Javascript

Without the semicolon [1,2,3,4,5,6] will be evaluated as property access. Which is perfectly fine JS, I personally don't think that adding semicolons is such a big deal so I keep using them.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionseven11View Question on Stackoverflow
Solution 1 - JavascriptEversView Answer on Stackoverflow