Trouble understanding how the argument got passed with the following argument in this example?

This answer works, but I’m unclear about why. What makes the argument ‘name’ pass with the ‘Cole’ argument below? I don’t see the rationale. Thank you for helping me understand this correctly.

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

See half-way down the page. Not much of an explanation given.

Capture

Hey there.

If this is stumping you, you might want to spend a bit more time reviewing functions more broadly. However, here’s what’s happening.

function sayThanks(name) is the declaration of the function name and its expected parameters. name is the only expected parameter.

{ console.log("Thank you for your purchase " + name + "! We appreciate your business."); } is the codeblock for the function. Here we are taking the parameter, name, and concatenating it into a string which we then output using console.log. So, whatever we pass to that name parameter becomes part of the string.

sayThanks('Cole'); is where we are calling the function, passing 'Cole' as the value for the name parameter. This is why, in the output, you see “Cole” in the printed string.

Does that make sense?

Yes, it makes sense. It seemed to have no rhyme or reason, but now I know how it works. Thank you for taking the time to answer my post!

1 Like

No problem. :slight_smile:

Let us know if there’s anything else you’re stuck with. :+1:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.