CKTIS / JavaScript
var — variable declaration
The `var` keyword declares a variable whose scope is not limited by an ordinary block. A new binding usually starts with `undefined`, but a separate binding in a function body can instead start with the value of a same-named parameter. A declaration without an initializer leaves an existing binding’s value unchanged. An initializer assigns a value only when execution reaches the declaration.
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
var name;
var name = expression;
var first = expression, second = expression;var is a reserved keyword that declares variables; it cannot be a variable name. A declaration can omit an initializer or contain several variables separated by commas.
An ordinary block in a function body does not limit the scope of var: a variable declared in an if block remains accessible outside that block within the same function. A var declared in a class static initialization block, however, is scoped to that block, even if the class is declared inside a function. At the top level, scope depends on the context, such as a script or module.
var bindings are available before statements in their scope execute. This behavior is called hoisting; it does not mean code is physically moved. A new binding is usually initialized to undefined. A var declaration can also reuse an existing binding, such as a same-named parameter in a function with a simple parameter list. With function f(x) { var x; return x; }, f(7) returns 7, not undefined. A default parameter, however, can cause a separate binding to be created in the function body and initialized with the value of the same-named parameter. With function f(x = 7) { var x; return x; }, f() returns 7: the body binding starts with the parameter’s value but is distinct from the parameter binding. An initializer in a var declaration performs its assignment only when execution reaches the declaration.
Redeclaring the same variable with var is allowed; without an initializer, it does not reset the value. Unlike var, let and const are block-scoped and cannot be read before initialization. Do not assume that replacing these keywords preserves program behavior.
Examples
function demo() {
console.log(value);
if (true) {
var value = 7;
}
var value;
console.log(value);
}
demo();Result
undefined 7
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.