String Passed As Function Parameter Is 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
Solution 1:
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).
Solution 2:
Why is that?
Because you don't pass any argument. Try the following:
test(a);
The following definition:
lettest = (a) => console.log(a);
is like the following:
functiontest(a){
console.log(a);
}
So when you call test, without passing any argument the value of a would be undefined.
Post a Comment for "String Passed As Function Parameter Is Undefined"