What purpose does name have in the following statement?
var myArray =[], name;
I usually initialize my arrays as the following:
var myArray =[];
What purpose does name have in the following statement?
var myArray =[], name;
I usually initialize my arrays as the following:
var myArray =[];
You are actually initialising two variables there, myArray
and name
.
You set myArray
to []
and name
to undefined
, because you don't give any value.
Your code is equivalent to this:
var myArray = [];
var name;
In JavaScript multiple variable assignments can be separated by commas, eliminating the need for repetitive var statements.
var myArray = [];
var name;
is equivalent to
var myArray = [], name;
It doesn't affect myArray
, it's just the same thing as
var myArray = [];
var name;
It's essentially the instantiation of a second variable name
with no value.
The recommended way (clean and short as possible) of writing this kind of code is the following:
var myArray = [],
name;