Parameter-Confusion
You can pass numbers or you can pass variables that contain the numbers to the functions as a parameter.
function double(number) {
return number * 2
}
// create a variable grade
let grade = 10
// then pass it to double()
double(grade) // returns 20 (because grade is 10)
Note that grade
and number
aren't the same. This is what we call variable scope. One variable contains one value inside and the other, outside the function.
Declaring a variable with the same name as a parameter
Number is what? Try not to do this until you get used to distinguishing between the variable and the parameter.
function double(number) {
return number * 2
}
let number = 10
double(number) // returns 20
number = 50
double(number) // returns 100
Recap
The parameter is only available inside the function. It may be confusing when the name of a variable is the same as a parameter, but remember that they are NOT connected. To avoid this confusion, you can avoid naming variables the same as parameters in the first few weeks of your programming journey.