I just watched a video on best practices for JavaScript. Now I have always been in the camp of "Use semicolons!" and this video didn't change it. There are so many errors that can creep in if you don't know the rules of how ASI works. For instance:
console.log("Log")
[1,2,3].forEach(function(value) {
// stuff
})
This previous line of code will cause an error because brackets are perfectly acceptable after an object as far as JavaScript is concerned.
Another thing brought up was using curly braces on the same line of the function or anything else that is using them because if you try to do something like this:
function() {
return
{
x: x
}
}
You will get an error because ASI will put a semicolon right after return. The way to fix this would be to put the curly brace on the same line as return. As an aside, this means to put curly braces on the same line for everything to keep things consistent.
Another reason the author suggested for using semicolons was to stay consistent with other C-like languages because JavaScript itself is a C-like language.
I don't want to tell you how to live your life but I would sure like to make things easier on you so USE SEMICOLONS!...Please.
Update:
In case you needed more convincing, this is from the Google JavaScript style guide: Every statement must be terminated with a semicolon. Relying on automatic semicolon insertion is forbidden. Semicolons are required
No comments:
Post a Comment