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! While you're at it, check out some resources Treehouse students have shared here.
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 trialDennis Trinidad
Full Stack JavaScript Techdegree Student 759 PointsVariables 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
1 Answer
Bella Bradbury
Front End Web Development Techdegree Graduate 32,790 PointsHi Dennis!
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 let
but instead deals with the scope of the variable. Here's a comparison of the three ways to define a variable in JavaScript.
You also mentioned Treehouse videos about defining variables. Here are some of the available resources:
-
JavaScript Basics Course
- The "Storing and Tracking Information with Variables" section is 9 steps and is very informative on this topic.
-
Introducing JavaScript Course
- Takes you through JavaScript fundamentals while creating a game.
-
- A jumpstart look into the fundamentals of JavaScript.
-
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.
Happy coding!