Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Variables Let, Const
it wasnt running when I had instead of let correctAnswers = 0 const correctAnswers = 0
why we can have const in questions but not in correctAnswers.
Would you mind to refer me to a video where is it explained
It's a good idea to post your complete code when posting a question, this helps us to make sure that we're able to fully resolve your issue. However, I believe I can still help via context clues.
The reason that
const wouldn't work in this situation is because it's a constant variable. This means that it can't be reassigned or redeclared. Think of it like the value of your birthday, it's never going to change because it's a fixed piece of information.
let will work because it's a re-assignable variable. In our birthday analogy, this would be your age. It's a piece of information that shouldn't be locked in because it will be changing.
When used properly the syntax of these two variables would look like this:
const birthday = "January 1, 2000"; let age = 0; // my age in 2005 age = 5; //my age in 2017 age = 17;
See how we only declare the birthday variable once, but are able to reassign the age variable? That's the difference between
let and 'const'.
There is also a third type of variable,
var which is very similar to
You also mentioned Treehouse videos about defining variables. Here are some of the available resources:
- The "Storing and Tracking Information with Variables" section is 9 steps and is very informative on this topic.
Defining Variables with 'let' and 'const' Workshop
- Once you feel more confident with JS variables, here is a workshop you can use to reinforce the information.