I recently came across the following line of code in a JavaScript book that I am working through:
var col = [], top, bottom;
This is the first time I’ve encountered a variable seemingly being given three variables. Could someone explain what is happening in this line of code?
It is simply a shorter version of this:
There is no real advantage/disadvantage of one style over the other, but JSLint likes to have all
vardeclarations in each scope combined (as you have in your question):For a full explanation of why this is the case, you can have a look at the ECMAScript spec. Here’s the relevant part of the grammar:
It’s also worth noting that the commas used here are not the same as the comma operator. It just happens to use the same character.