Variable runs as "undefined" within a function -- why?

I am working through the lesson on functions.

When coding a simple function I first instinctively coded a variable as “name” to input a customer name into a thank you message function. This produced a result of “undefined.” When I ran the function with an argument of the exact name, it worked in this place.

Is there a set rule that variables cannot be predetermined and then used in function arguments? I want to make sure that I am clear on the logic of the language.

Thanks!

Just to clarify, is the code in the screenshot the code that is producing the undefined? I tested this exact code and got no such message, so just want to be sure where the issue is arising before being able to help!

Hi there, sorry I should have included two versions with results when I run it. I first tried to call the function after declaring the variable name as ‘Cole’ and I received an undefined result for “name” in the function.

I am unclear why I need to declare the name in the argument of the function and why my variables cannot be input into the function.

Let me know if this still doesn’t make sense!

Declaring the variable in the global scope as above should call the function correctly, see my example below:

var name = 'Cole'

function sayThanks(name) {
  console.log('Thank you for your purchase ' + name + '! We appreciate your business.')
}

sayThanks(name)
# prints Thank you for your purchase Cole! We appreciate your business.

The best thing I can think of is that you called the function inside of a console.log() like the following:

console.log(sayThanks(name))

This would print the same message as above, but also print an undefined. I think more than likely we would need the exact code that you ran to produce the undefined, as from what you’ve told me it seems like everything should be okay!

Thanks for the responses!

This is the problem I am encountering. If I run the function without retyping the argument name when calling the function, name prints as undefined. By rule do I need to type the variable as the argument, not just in the predefined action of the function?

Ah okay I see the issue! So parameters do not default to taking the values of variables with the same name, this is to avoid possible unintended consequences of having the variable environment being polluted. As you have specified that the function needs to take a parameter name, any references to name inside of the function body will be in reference to the parameter and ignore any external variables, even global ones. Therefore when you call sayThanks(), you are passing undefined as the name argument, and that is why you end up with the result you have.

There’s a couple of ways to resolve this. If you always want to use the global variable name, you can either change it and set it as a default argument like so

var user_name = 'Cole'

function sayThanks(name = user_name) {
  console.log('Thank you for your purchase ' + name + '! We appreciate your business.')
}

sayThanks()
# prints Thank you for your purchase Cole! We appreciate your business.

or else you can just remove the parameter completely, like below

var name = 'Cole'

function sayThanks() {
  console.log('Thank you for your purchase ' + name + '! We appreciate your business.')
}

sayThanks()
# prints Thank you for your purchase Cole! We appreciate your business.

Personally, I would recommend just using a parameter and calling it with sayThanks(name) as in large programs it can help you to keep track of what is being read into function calls, however either of the above two code blocks will work just the same! I hope this helped and if you have any further questions please do ask.

3 Likes

Thanks so much! This is much clearer and I appreciate being able to visualize a couple of different ways that this can be flipped around. I understand the interactions of the elements and how they run much better now!

2 Likes