0

Beginner here. Why userName is undefined in the function and how to make the function to have it as John Doe and not undefined?

const userName = 'John Doe';
console.log(userName);
const loggedInUser = (userName) => console.log('Logged in user is: ' + userName);
loggedInUser();

Console output:

index.js:22 John Doe
index.js:23 Logged in user is: undefined
3
  • 1
    You're hiding the "outer" userName with the userName parameter of the function. Commented Feb 4, 2018 at 9:19
  • 2
    An example of variable shadowing in javascript Commented Feb 4, 2018 at 9:26
  • FWIW, this is not a property, it's a "variable" though it would be more correct to say that it is a constant (since its value cannot change). Commented Feb 5, 2018 at 3:33

1 Answer 1

1
const loggedInUser = (userName) => console.log('Logged in user is: ' + userName);

defines a function named loggedInUser that takes a single argument.

After defining this function you call it on the next line loggedInUser();, but you don't provide the argument.

Try loggedInUser(userName);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.