FAQ: Functions - Parameters and Arguments

HELP! Parameters

const rectWidth = 5;
const rectHeight = 2;
function calculateArea(rectWidth, rectHeight) {
console.log(rectWidth * rectHeight);
}
calculateArea();

QUESTION:

  1. Why do I keep getting NaN as answer to the above code?
  2. For functions, is it a rule to “const” to declare a variable? why not let or var?

Your function declaration states that calculateArea() takes 2 parameters, rectWidth and rectHeight. We need to pass in arguments for these parameters whenever we call the function.

We use const to declare variables that are constants. This means that their values never change. If you tried to change the value of a const, you would get an error. We use let when we declare variables whose values do change. Both const and let were introduced with the release of ES6. var is a way of declaring variables before this and is mostly used for backwards compatibility. It also behaves a little differently. I find this article explains the differences between the three well.


Side note: please use the </> to format your code in future posts.

Isn’t this code exactly the same ?

function sayThanks(name) {

  console.log('Thank you for your purchase ${name} ! We appreciate your business.');

}

No? you have a space between name and exclamation mark.

furthermore, for template literals (${name}) to work, the string needs be enclosed in back-ticks/grave accent

I think it’s been told multiply times. But why don’t you suggest using this code with inserting variable inside string introduced in ES6?

`Thank you for your purchase ${name}! We appreciate your business.`

es6 should be preferred, but the exercise might not be able to cope

A question I have about this particular bit of code that I cant wrap my head around is: Why does the var “name”, used in the concantenation, not need to be declared? is it declared in the declaration of the function?

Is it that, anything that gets used as the arguement for sayThanks() gets assigned to name? regardless of whether you pass a string or a number?

function sayThanks(name) {

console.log('Thank you for your purchase '+ name + ‘! We appreciate your business.’);

}

sayThanks(“Cole”);

parameters (like name) are declared with the function, the parameter(s) get there value(s) from the argument(s) at function call.

exactly, this is the problem with a dynamic language like JavaScript, where we can just pass any data type. We could handle this at run-time, but this becomes tedious very quickly. Using TypeScript is a good way to overcome this problem

Thank you so much, that was just eating at me! Also that is the worlds speediest reply ever, Keep it up dude!

1 Like

From what I read in the tutorial, the argument can be either a variable or a value. I decided to go with the variable, after declaring it as ‘Cole’. Yet in the solution, it expects me to call the value.

Did I not understand the concept here?

Welcome to the forums!

Because the instructions don’t state to create and pass a variable to the function, the expected solution is different from yours. In step 3, one is expected to pass the string literal 'Cole' to the function. Your solution is valid and has the same final result, but it will be marked as wrong since the instructions didn’t state to use a variable.

1 Like

so in the second exercise of instruction, when do we use
'Thank you for your purchase '+ name + ‘! We appreciate your business.’
instead of
Thank you for your purchase ${name} ! We appreciate your business. ?
because the output is the same.

the latter is the template literals, which where introduced in 2015, which preferable should be used unless es6 is not supported and you can’t transpile

thank you for your reply <3

Hello everyone.
So while practicing, this is what I typed in VS code and I can’t seem to understand why the variable name has a strikethrough on it.
Please help.

image

usually if you then hover or hold your mouse on the strike-through, VScode should tell you why

Said deprecated so instead of ‘name’, I changed to ‘myName’ and there was no strikethrough.
However, I really didn’t understand why it worked as it did. I still don’t.
Still researching it.

Horrible excercise if you are brand new to programming. no matter what I did I couldn’t get declaring the function sayThanks(‘Cole’) to work and kept getting errors. I tried it before the function declaration, inside the body after the first bracket {, nothing worked and the hint was pointless. It wasn’t until I looked in here and saw one of the code examples had 2 spaces after the body of the code after the final } that I tried hitting enter twice and then putting sayThanks(‘Cole’); and it worked. Nowhere in any lesson did it tell me that declaring the function had to be at least 2 spaces before the function sayThanks(name) declaration or 2 spaces after the final bracket? are we just supposed to know this through osmosis?

Do you still have both code samples? I would like to have a look at them