Variables
A variable is a name bound to a value. In JavaScript you create one with let, const, or var, and the keyword you choose decides where the name is visible and whether it can be reassigned.
Detailed Explanation
Think of a variable as a label, not a box. The label sessionCount points at a value. Assignment moves the label. For primitive values such as numbers and strings, the value itself sits in the binding, and copying the variable copies the value. For objects, arrays, and functions, the binding holds a reference, and copying the variable copies only the reference, so both names end up pointing at the same object.
Three separate things happen around a variable, and mixing them up causes most beginner confusion. Declaration introduces the name. Initialization gives it a first value. Assignment gives it a later value. let total; declares without initializing, so reading total gives undefined. const total = 0; declares and initializes in one step, and const requires that step, because you can never assign to it again.
The keyword matters more than the name. var is function-scoped, is hoisted to undefined, and can be redeclared. let and const are block-scoped, live in the Temporal Dead Zone until their line runs, and cannot be redeclared in the same scope. New code should use const by default and let when the value genuinely changes. var exists for compatibility with pre-2015 JavaScript.
const prevents rebinding, not mutation. This trips up almost everyone. const user = { name: "Ana" } forbids user = somethingElse, but user.name = "Bea" is allowed, because the object is not frozen — only the binding is fixed. If you need the value itself locked, Object.freeze gives you a shallow freeze, and deep immutability needs a library or a discipline.
A name that has never been declared is a hazard. In sloppy (non-strict) code, sessionCount = 1 with no keyword creates a property on the global object, which means a typo inside a function can silently overwrite unrelated state. In strict mode, and therefore inside every ES module, the same line throws a ReferenceError. That is one of the strongest arguments for always working in modules.
Identifier rules: names may contain letters, digits, $, and _, and may not start with a digit. They are case-sensitive, so userId and userID are two different variables. Reserved words such as class and return are not allowed. Convention is camelCase for variables and functions, PascalCase for classes, and SCREAMING_SNAKE_CASE for values that are genuinely fixed configuration.
The cost of a variable is mostly readability. Every mutable binding is one more thing a reader has to track through a function. Preferring const, declaring names close to where they are used, and keeping scopes small does more for maintainability than any clever trick with the keywords themselves.
Code Examples
Primitives copy the value. Objects copy the reference.
// Primitives copy the value. Objects copy the reference.
let a = 10;
let b = a; // copies the number
b += 5;
console.log(a, b); // => 10 15
const first = { plan: "free" };
const second = first; // copies the reference, not the object
second.plan = "pro";
console.log(first.plan); // => pro (one object, two names)
console.log(first === second); // => trueconst freezes the binding, never the contents.
// const freezes the binding, never the contents.
const limits = { maxUsers: 5 };
limits.maxUsers = 50; // allowed: mutating the object
console.log(limits.maxUsers); // => 50
// limits = { maxUsers: 100 }; // TypeError: Assignment to constant variable.
const locked = Object.freeze({ maxUsers: 5 });
locked.maxUsers = 50; // silently ignored in sloppy mode, TypeError in strict mode
console.log(locked.maxUsers); // => 5Common Gotchas
A missing keyword is not a local variable
`count = 1` without `let`, `const`, or `var` creates a global in sloppy scripts and throws `ReferenceError: count is not defined` in strict mode and in modules. Always declare.
`const` stops reassignment, not mutation
a `const` array can still be pushed to and a `const` object can still gain properties. If you need the data to stay put, freeze it explicitly.
Declared but uninitialized is `undefined`, not an error
`let retries;` gives `retries` the value `undefined`. That is different from a name you never declared, which throws on access.
Reading a `let` or `const` before its line throws
the binding exists from the start of the block but is unusable until initialized, so you get a `ReferenceError` rather than `undefined`.