Say I have a function dosomething
and I call it sometimes with an argument, sometimes without.
I want to let the default value of the argument equals 5. So I have these two ways:
function dosomething(a) {
if(!a) {
a = 5;
}
}
Or
function dosomething(a) {
if(!a) {
var a = 5;
}
}
After testing I found they both work.
But do I have to use the var
? What's the difference?