Why the following code doesn't alert undefined?
function test(param){
var param = param;
alert(param);
}
test("SO"); // alerts SO
How is that the param that is being assigned to the local variable is matched with the argument of the function, and not with the local variable itself?
Does right values have a "matching preference" for the the function arguments or what is the cause?
There are two parts in
var param = param;
The first one is the var declaration :
var param;
The second one is the assignement :
param = param;
The var declaration does nothing, as the variable already exists (the scope of a variable is the whole function call). And the assignement does nothing, as it keeps the same value.
You could check that by assigning a different value :
function test(param){
console.log('1', param) // logs "SO"
var param = param+"4";
console.log('2', param) // logs "SO4"
}
test("SO");