by jmoiron · 13y ago · 0 comments
var i=1
, j=2
, k=3
You can remove any of the comma prefixed lines there (even the last one) and not introduce an error. You can add another similarly prefixed line anywhere to the list and not introduce an error. It's obvious if a comma is missing (which is good, because you don't have a compiler to let you know).It can be difficult to spot the lack of a trailing comma, or the end of this declaration list having a comma instead of a semicolon (, vs ;), both of which will break the execution of your script.
So please, do not change your code to make it look better without understanding why it's like that in the first place.
The classic example is tchanging the following to allman/gnu style braces would break it in JavaScript:
// works, returns {a:1,b:2}
return {
a: 1,
b: 2
}
// semicolon inserted after return, returns undefined
return
{
a: 1,
b: 2
}Indeed many tools (IDEs) do correct these kinds of problems, and it strikes me as silly that we have to worry about the execution of programs failing because of these types of typos/bugs.
That is wrong. The comma is not incidental, it is an operator that tells you the next declaration is locally scoped. A missing comma changes the result of all following assignments.