In JavaScript, is there any benefit in assigning a temporary value to a newly declared variable? For example…
var a = 0,
b = '';
// Somewhere else
a = 5;
b = 'Goodbye';
vs
var a, b;
// Somewhere else
a = 5;
b = 'Goodbye';
I’m aware that assigning a variable on declaration will set its type. But in JavaScript, this can be easily changed by assigning a value of a different type, so doesn’t really protect it in any way.
What are the advantages/disadvantages of the above?
I believe that the advantage is: Order in your code.
So you’ll know right from the beginning of the code which type is which var.
The disadvantage is more code lines and size of the file.