Puzzled by const use

Tell us what’s happening:
Hi there, I’m just looking for clarification. Correct me if I am wrong but I don’t recall any reference to const before this challenge. I understand I’m probably supposed to search about it (in fact I did) but I would have appreciated a lesson on it before this challenge.
At first, it seemed to me that the else part of the example code was continuously reassigning a value to const, which, if I am correct, is forbidden, but then it occurred to me that that happens in different blocks, and thus allowed. Am I correct? Please enlight me :smiley:

Your code so far


// Only change code below this line
function countdown(n){
return;
}
// Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:78.0) Gecko/20100101 Firefox/78.0.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

So, const is interesting. Bear with me here.

In JavaScript, a variable holds a single value. For single numbers, this is straightforward. The single number is stored there. For a whole array, we can’t store the whole array in a single value. So instead the array is written somewhere in memory, and the location of the array is written into the variable.

Now, const means the value stored in the variable will not be changed. For numbers, this is easy. The number won’t change. For array, it is a bit different. The memory location won’t change, which is to say, you cannot change which array you are looking at or where it is stored, but you can modify that array.

2 Likes

As i know the const cannot be changed , what’s the real problem you found , i mean can you show your code.

Thank you @JeremyLT, I think I got it.
But I have another question, if I may. Is there any particular reson behind the use of const in this example? I’ve tried using a

var = countArray;
and deleting const in the code and that works too.

No code so far, @mehdimasmar, I was just trying to figure out the reason behind const

var can have some strange behavior. In general

  1. Use const if you can. This is for variable data that won’t change.
  2. Use let if you must. This is for variable data that must change.
  3. Use var never.

The freeCodeCamp curriculum was written before const and let were part of JavaScript. Nowadays, it is recommended that you always use let and const, but when freeCodeCamp was first written, we only had var. We’re always iterating and updating the curriculum.

Thank you, now it’s clearer. I’ve just started with JavaScript here at freeCodeCamp, and I didn’t know all this.

It’s a bit of a mistake to have const in the example code considering it has not been taught yet. But such inconsistencies can happen over time as challenges are updated (PR with updates). It really has to do with the age of the curriculum and how it has been updated over time.

You will learn about let and const in the next part of the curriculum (ES6) which is only one challenge away.

2 Likes

Yes, I’m looking forward to it!
I think that these kinds of inconsistencies are more than understandable, considering how fast JavaScript evolves. Plus, they keep us focused. Every now and then they could be even useful. Like, ‘find the error in the code below’ challenge series. :grin:

2 Likes

Keeping the order of how concepts are introduced can be challenging with such a large amount of challenges. But in this case, I think it’s just a bit of an oversight.

The example code is more “correct” using const from a best-practices perspective, but “incorrect” from a pedagogic point of view. When you are more experienced it’s easy to forget how little things like using a variable declaration that hasn’t been taught yet can throw someone off that has just started to learn.

It may seem like a small thing to someone experienced but for new learners, it can be very jarring and just introduce confusion and an extra mental burden for no good reason. In my opinion, it probably should be fixed. Really, by now, const and let should be part of the basic JS part of the curriculum (likely will be in time).

Jokes apart, you’re obviously right. It was a little jarring seeing const all of a sudden without having encountered it before. Maybe (as recursion was already treated in a previous challenge) a little explanation in the ‘Use Recursion to Create a Countdown’ lesson could suffice for the moment, leaving a deeper exposition for the following ones. But then again, if const and let should be treated earlier in the curriculum that won’t be necessary.