When I pass a string to a function as a parameter, it returns undefined. Why is that?
let a = 'b';
let test = (a) => console.log(a);
test(); // undefined
When I pass a string to a function as a parameter, it returns undefined. Why is that?
let a = 'b';
let test = (a) => console.log(a);
test(); // undefined
Why is that?
Because you don't pass any argument. Try the following:
test(a);
The following definition:
let test = (a) => console.log(a);
is like the following:
function test(a){
console.log(a);
}
So when you call test, without passing any argument the value of a would be undefined.
When you call test(); you aren't putting anything between ( and ), so you aren't passing any parameters.
When you defined test (with (a) =>) you created a local variable a that masks the global variable with the same name.
To pass a you have to actually pass it: test(a).