let vs const

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.

let-vs-const.js
const siteName = "FreeCodingSchool";
const pi = 3.14159;
siteName = "Other"; // ❌ Error: cannot reassign a const

Here 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.14159 creates a second constant holding a number.
  • siteName = "Other" tries to give siteName a new value, and because it is a const, 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-vs-const.js
let score = 0;
score = score + 10; // βœ… allowed
console.log(score); // prints: 10

Here we create a value with let and then update it:

  • let score = 0 creates a variable that starts at 0.
  • score = score + 10 reassigns score by adding 10 to its current value, which let permits.
  • console.log(score) prints the new value, 10.

A common place you will use let is a loop counter, which changes on every step:

let-vs-const.js
for (let i = 0; i < 3; i++) {
console.log(i); // prints 0, 1, 2
}

Here the loop counter changes on each pass:

  • let i = 0 starts the counter at 0.
  • i < 3 keeps the loop running while i is less than 3.
  • i++ adds 1 to i after each pass, which only works because i is a let.
  • console.log(i) prints the counter each time, giving 0, 1, and 2.

🧊 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.

let-vs-const.js
const user = { name: "Alex" };
user.name = "Sam"; // βœ… allowed - we changed a property, not the variable
console.log(user.name); // prints: Sam
user = { name: "Sam" }; // ❌ Error - this reassigns the variable

Here 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 a name property.
  • 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 printing Sam.
  • user = { name: "Sam" } tries to point user at 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!

  1. Create a const called birthYear and a let called age.
  2. Reassign age to a new number and print it.
  3. Try reassigning birthYear and read the error in the console.
  4. Create a const object with a name property, then change that property and print it.

🧩 What You’ve Learned

  • βœ… const cannot be reassigned, let can
  • βœ… Use const by default and let only when the value will change
  • βœ… const blocks reassignment but does not freeze object or array contents
  • βœ… Avoid var in 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.

Share & Connect