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?
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 :)).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With