2.4 Let & const
Hello everyone. In this video we talk about let and const, two keywords you are going to use everywhere in this React course. Both are ways to declare variables in modern JavaScript. You probably already know the older var keyword: it works, but ES6 introduced let and const as more precise and safer alternatives, and from now on you should reach for them by default.
let, const and var in practice
Use let for a value that really changes during the program, the way var used to be used. Use const for a value you assign once and never reassign, which actually happens much more often than people expect. In this course you will mainly see const, with a few let where reassignment is needed; var will not appear at all.
To play with the difference quickly, jsbin.com is convenient. Disable the HTML output and keep only the JavaScript panel plus the console. We declare a variable with var myName = "Matthieu" and log it: the console prints Matthieu. We can reassign it later to "Lucie" and log again, no problem. Replacing var with let changes nothing visible: it still allows reassignment.
var myName = "Matthieu";
console.log(myName);
myName = "Lucie";
console.log(myName);
let myAge = 30;
myAge = 31; // OK
const PI = 3.14;
PI = 3.15; // TypeError: Assignment to constant variable
The interesting case is const: as soon as you try to reassign a constant, JavaScript throws an error like "Assignment to constant variable". That looks annoying at first, but it is actually a feature: it catches bugs where you accidentally overwrite a value that was meant to stay fixed. With let and const in your toolbox you have a clearer, more modern way to describe what each variable is supposed to do.
Summary
This lesson explains the critical difference between JavaScript's var, let, and const keywords introduced in ES6. While var has been the traditional approach for declaring variables, modern JavaScript favors let for mutable values and const for constants that should never be reassigned. The lesson reinforces that const provides safety by throwing an error on accidental reassignment, helping developers catch mistakes early and write more intentional, maintainable code.
Key points
- var is legacy syntax; let and const are the ES6 standard replacements that developers should use instead
- let declares variables that will change (mutable), while const declares values that remain constant throughout the program
- const prevents accidental reassignment by throwing an error, acting as a safeguard that improves code quality and catches bugs early
- Modern best practice: default to const for all values, use let only when you know the variable will be reassigned
- Block scoping with let and const provides more predictable behavior than function-scoped var
FAQ
What is the main difference between let and const?
let is used for variables that will be reassigned, while const is for values that remain constant. If you attempt to reassign a const variable, JavaScript throws an error—this prevents accidental modifications and makes code intent clearer.
Why should I stop using var in modern JavaScript?
var has confusing function-scoping behavior and lacks safety checks. let and const offer block-scoping (more predictable) and const adds error protection against accidental reassignment. Modern codebases should use let and const exclusively.
Should I default to const or let when writing new code?
Default to const for every variable. Only use let when you explicitly know the variable will be reassigned. This practice makes code intent transparent—readers immediately see which values are intended to change versus remain constant.