Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of semicolons in ES6 [duplicate]

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?

like image 689
seven11 Avatar asked Jan 22 '16 15:01

seven11


People also ask

Should I use semicolons in ES6?

Even Google's summarized ES6 style guide continues to require semi-colons. There is a good reason. As developers we should know that when you remove a feature there will inevitably be users who relied on that feature to help them accomplish their task (in this case, we all program and write code :)).

Is it good to use semicolons in JS?

Semicolons are an essential part of JavaScript code. They are read and used by the compiler to distinguish between separate statements so that statements do not leak into other parts of the code. The good news is that JavaScript includes an automatic semicolon feature.

Is semicolon mandatory in react JS?

It's okay to not use semicolons in JS. It's not slower or introduces bugs and it's okay to use them too. It doesn't matter.


1 Answers

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.

like image 66
Evers Avatar answered Oct 11 '22 22:10

Evers