CKTIS / JavaScript
Variables: let, const, and var
A variable binds a name to a value. Learn how declarations and initialization work, how let, const, and var differ, and how scope and initialization determine when values are accessible.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
let name;
let count = 1;
const settings = { enabled: true };
var legacyCount = 1;
count = 2;A variable is a named binding to a value. A declaration introduces the name, initialization gives it its initial value, and later assignment changes the binding's value. JavaScript is dynamically typed: a variable declared with let can later be assigned a value of another type.
let and const are block-scoped. let allows reassignment; let name; initializes the variable to undefined when execution reaches that declaration. An ordinary const declaration requires an initializer and prohibits reassignment, which throws TypeError if attempted. It does not freeze an object: settings.enabled = false can change its property.
var does not create scope in an ordinary {} block: inside a function, it belongs to that function's scope. When var introduces a new name in a function, the binding is initialized to undefined before the function body executes. A declaration without an initializer does not change the value of an existing binding. For example, function f(x) { var x; return x; } returns the supplied argument because var x; reuses the existing binding for the parameter x. If a var declaration has an initializer, it runs only when execution reaches the declaration. let and const bindings are also created earlier, but remain in the temporal dead zone (TDZ) until initialized. Reading a binding in its TDZ, including through typeof, throws ReferenceError.
Redeclaring a name with let or const in the same scope is a syntax error. An inner block can instead declare its own name that shadows the outer one. In the example, the inner count does not change the outer one, but step, declared with var, remains accessible within the function after the block ends.
Examples
function demonstrateVariables() {
let count = 1;
const settings = { enabled: true };
{
let count = 10;
var step = 2;
console.log(count);
}
count += step;
settings.enabled = false;
console.log(`${count} ${settings.enabled}`);
}
demonstrateVariables();Result
10 3 false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.