let vs const
Table of Contents + β
In the previous lesson, we learned how to create variables. Both let and const create variables, so now letβs understand the difference between them and when to use each.
βοΈ The Main Difference
The difference comes down to one thing: whether the variable can be reassigned.
| Keyword | Can be reassigned? | Use it when |
const | No | The value will not change |
let | Yes | The value will change later |
π Use const by Default
Reach for const first. It tells anyone reading your code that this value will not change, which makes the code easier to understand and prevents accidental changes.
const siteName = "FreeCodingSchool";const pi = 3.14159;
siteName = "Other"; // β Error: cannot reassign a constHere we declare two const values and then try to change one of them:
const siteName = "FreeCodingSchool"creates a constant and locks it to the text"FreeCodingSchool".const pi = 3.14159creates a second constant holding a number.siteName = "Other"tries to givesiteNamea new value, and because it is aconst, JavaScript throws an error instead of allowing the change.
π Use let When the Value Changes
Use let only when you know the value will need to change, such as a counter or a score.
let score = 0;score = score + 10; // β
allowedconsole.log(score); // prints: 10Here we create a value with let and then update it:
let score = 0creates a variable that starts at0.score = score + 10reassignsscoreby adding10to its current value, whichletpermits.console.log(score)prints the new value,10.
A common place you will use let is a loop counter, which changes on every step:
for (let i = 0; i < 3; i++) { console.log(i); // prints 0, 1, 2}Here the loop counter changes on each pass:
let i = 0starts the counter at0.i < 3keeps the loop running whileiis less than3.i++adds1toiafter each pass, which only works becauseiis alet.console.log(i)prints the counter each time, giving0,1, and2.
π§ const With Objects and Arrays
This part surprises many beginners. With const, you cannot reassign the variable, but you can still change the contents of an object or array it points to.
const user = { name: "Alex" };
user.name = "Sam"; // β
allowed - we changed a property, not the variableconsole.log(user.name); // prints: Sam
user = { name: "Sam" }; // β Error - this reassigns the variableHere we change an objectβs contents and then try to reassign the variable itself:
const user = { name: "Alex" }creates a constant pointing to an object with anameproperty.user.name = "Sam"changes a property inside that object, which is allowed because the variable still points to the same object.console.log(user.name)confirms the change by printingSam.user = { name: "Sam" }tries to pointuserat a brand-new object, which reassigns the variable and throws an error.
What const really protects
const stops you from pointing the variable at a new value. It does not freeze
the contents of an object or array. You can still add, change, or remove items
inside them.
π« Avoid var
You may see older code use var to create variables. var is the old way and has confusing behavior that causes bugs.
Stick to let and const
In modern JavaScript, always use const and let. Avoid var in new code.
β οΈ Common Mistakes to Avoid
| Mistake | Problem | Solution |
Using let for everything | Hides which values are meant to stay the same | Use const by default, let only when needed |
Expecting const to freeze an object | You can still change its properties | Remember const only blocks reassignment |
Using var | Old behavior that leads to bugs | Use const and let instead |
π§ Try It Yourself!
- Create a
constcalledbirthYearand aletcalledage. - Reassign
ageto a new number and print it. - Try reassigning
birthYearand read the error in the console. - Create a
constobject with anameproperty, then change that property and print it.
π§© What Youβve Learned
- β
constcannot be reassigned,letcan - β
Use
constby default andletonly when the value will change - β
constblocks reassignment but does not freeze object or array contents - β
Avoid
varin modern JavaScript
π Whatβs Next?
Now that you understand variables and how to declare them, we will look at the different kinds of values you can store. Letβs continue to Data Types.