Can you change parameters on constant functions?

I’m confused why I’m able to reassign the value of the constant variable plantNeedsWater in the following code:

const plantNeedsWater = function(day) {
  if (day === 'Wednesday')
    {return true}
  else {
    return false;
  }
}

console.log(plantNeedsWater('Tuesday'));

console.log(plantNeedsWater('Wednesday'))

//outputs false, true

I was imagining that by calling the function, initially plantNeedsWater evaluates to a constant ‘falsey’ boolean, but then by calling the function a second time, you can change it to a truthy. Does this effectively destroy the original plantNeedsWater value by overwriting it?

24 Likes

The only thing constant is the name and the assigned function. Parameters are not in the same scope. That’s why we can run the function over and over with different inputs.

121 Likes

Thanks for your reply. So to be clear, in the above code:

const plantNeedsWater = function(day){
}

is a function only, and not a variable declaration, right? Effectively the same as if I had typed:

function plantNeedsWater(day){
}

Can the function’s contents be changed later if I declared a function like:

let plantNeedsWater = function(day){
}

Through experimentation, I determined “yes” is the answer to the above questions. Thanks for your help!

6 Likes

No. The above is hoisted as a declared function; the preceding is hoisted as a variable, which expression will be caught on the next pass.

When we assign using = in our code, the right hand side does not get hoisted. That is why we call them function expressions; they appear on the right hand side of an equals sign. On that side they are values.

They can also appear in arguments to other functions, in which case we refer to them as a callback. Essentially when the return value of one function given the inputs becomes the argument value of another function, per chance with additional arguments (that may include the initial inputs in some way).

Hoisting and binding are two different things to keep aware of in JS.


 > (function () {
     return foo();
     function foo(day="Wednesday"){
       return day==="Wednesday" ? "Water me!" : "Don't water me!";
     }
   })()
<- "Water me!"
 >

 > (function () {
     return foo(arguments[0]);
     function foo(day="Wednesday"){
       return day==="Wednesday" ? "Water me!" : "Don't water me!";
     }
   })('Thursday')
<- "Don't water me!"
 > 

The binding is to the return (response) value of the expression. That’s the key. Only foo was ever hoisted. Least not to mention is the arguments object which may or may not be populated. The first value may be of some interest…


This tells us that hoisting occurs within functions, not just at the global level. Whatever isn’t hoisted is treated as a value on the second pass. Recall that expressions are values, and utilize that going forward.

23 Likes

So the const only means that the declared function can not be renamed, nor can the parameters be changed. Only the arguments that are passed onto the parameters can change?

8 Likes

Correct. They are the values being passed to the function. A parameter has no intrinsic value, only the one handed to it with each call.

Later we will learn how to give a parameter a default value that may be overwritten with an argument, but which will be the value used in the function if the positional argument is missing from the call. This will make more sense when the topic is covered so don’t give it another thought, for now.

10 Likes

@mtf What I understood is that variables declared with const have a constant binding. This means that if we declared a constant variable with a value of an object, we can mutate the object, but we can’t change the binding of the variable to the object. E.g. assign a new object to the variable. However, primitives can not be mutated meaning that once you assign a primitive to a constant variable, it cannot be changed

const obj = {};

obj.foo = 2;
obj.foo; // 2

obj = [1, 2, 3]; // TypeError: Assignment to contant variable

const bar = 5;

bar.foo = "test";
bar.foo; // undefined

bar = 2; // TypeError: Assignment to constant variable
1 Like

I can’t even get past the first part of the excercise. I type
const plantsNeedWater = function(day) {
}
…and it keeps telling me I’m wrong and I cannot advance to the next portion of the code. Getting really frustrated because the error message isn’t clear on what I’m doing wrong. Can someone enlighten me??

Suspect a naming error. If the SCT is looking for plantNeedsWater then it won’t find it.

1 Like

Hello nice people! I was playing around and trying different things with the code and at the const plantNeedsWater = function(day){ } I put the day as a string => ‘day’ and I was trying with a number like 29. Both of these last cases gave me an error and I don’t know yet the answer why. Can you help me, please? Many thanks for considering my request.

Hi there, I have searched on the w3school website and found that the Parameter Rules include that JavaScript function definitions do not specify data types for parameters. if you enter a string or a specific number, it will return to SyntaxError: Unexpected number/string. :grinning:

Hope this link will help you:
w3schools js_function_parameters

1 Like

thank you so much for the link and your reply!