I have just completed the Silly story generator assignment and my question is how do I know, when it is best to use let or const.
After I finished the assignment I checked the code of the finished example (https://github.com/mdn/learning-area/blob/master/javascript/introduction-to-js-1/assessment-finished/main.js) and saw that in the solution both let and const are used and in my solution I only used let. My code still works as expected, but I am trying to understand how to best determine whether I should declare something as a variable by using let or as a constant by using const.
I understand the in theory different between variables and constants (one can be assigned a new value and the other can’t), but I find it hard to actually determine in code whether I should be using a variable or an constant.
Could someone explain on the example of the code of the Silly story generator why is there in some cases let and in some cases const used?
If you are the only one who creates an application, you don’t need to tell the difference because you know everything about the code. But if with someone, you should use const because someone may give the name you already gave to a variable. In which case, the application wouldn’t behave as you expect.
There’s a little bit difference between var and let actually, but we focus on let and const here.
In your GitHub example, I bet you never change the customName, randomize and story, for instance. This is why you used const. Similarly, I bet you never change the storyText too. So, you should use const for it to prevent overwriting.
Anyway, you don’t need to care for the short code like your example. In other words, for a big application, if you don’t care, you’d waste your time.
@safejourney This is a pretty good explanation, and quite similar to my thinking in terms of how I’ve been advising people who read through all my material. I’ve largely been explaining the differences, but telling people not to worry too much about it at this stage.
In my examples, I’ve largely been using const for things that are unlikely to be changed even in a bigger application, such as references to DOM element UI controls, but I’ve bene using let for things that might conceivably change as the application gets bigger, such as dynamically created list items.
In other words, not just considering the small examples in their current form, but also thinking about how they might change if they get bigger and more complex in the future.
So if I properly understand both you, @safejourney and @Rafael_Green for a beginner such as myself it is generally better to use const. If I were for example start working on some sort of group project I should definitely make sure to use const with variables to avoid any issues as @safejourney mentioned.
So best way is to try and ask myself will this variable be subject to change and if yes I should use let and if no I should use const?
And thank you all for answering. I understand the difference better now.