Quick Overview
`const` prevents reassignment, not mutation: a `const` object's properties can still be changed. `let` is scoped to its block, while `var` is function-scoped and leaks out of loops.
Imagine a variable as a cardboard box with a sticky note on the front. Referencing the variable name on the sticky note lets you access or replace whatever data is currently stored inside the box.
In modern JavaScript, choosing the right variable keyword (const vs let vs var) determines whether the data inside the box can be swapped out later and where in your program that box is visible.
JavaScript gives us three ways to label our cardboard boxes:
const (Constant): The sticky note is glued tight with superglue. You cannot reassign a new value to this variable after initialization. Always default to const!let (Reassignable): The box has a reusable lid. Use let when you know the stored value needs to change over time (such as loop counters, scores, or toggle states).var (Legacy): The old way of declaring variables (before ES6 in 2015). var lacks block scoping and behaves unpredictably due to hoisting. Avoid using var in modern codebases!// Always default to const for values that stay fixed
const maxConnections = 100;
// Use let when value reassignment is expected
let activeUsers = 0;
activeUsers = activeUsers + 1; // Valid reassignment (now 1)
console.log(maxConnections); // 100
console.log(activeUsers); // 1
const and let are block-scoped (contained strictly inside curly braces {}), whereas legacy var leaks out into outer functions:
[ Global Scope ]
├── const appName = "DevLoom"
└── Block Scope { }
├── const localSecret = "xyz" (Invisible outside block)
└── let counter = 5 (Invisible outside block)
const: Trying to reassign a const variable (pi = 3.14) throws an uncaught TypeError: Assignment to constant variable.const with Object Mutability: Declaring an object or array with const prevents reassigning the variable pointer, but you CAN still add or modify properties inside the object (user.name = "Sarah" is valid!).Write it yourself. The tests below run against your code.
Default to const everywhere. Switch to let only when reassignment is strictly needed. Next, let's look at combining variables with text using Template Literals!
Primitives copy by value and objects copy by reference, so `b = a` duplicates a number but shares an object. That single distinction explains most surprise mutations in a codebase.
Only backticks interpolate: `${user}` reads the variable, while the identical text in quotes prints literally. Strings are immutable, so every method returns a new one rather than editing in place.
Every JavaScript number is a 64-bit float, which is why `0.1 + 0.2` comes out as `0.30000000000000004`. `%` returns a remainder that keeps the sign of the left operand, so `-7 % 3` is `-1`.
Average 5.0 by 1 learner