What's the difference between variable definition and declaration in JavaScript?
Categories:
JavaScript: Understanding Variable Declaration vs. Definition

Demystify the core concepts of variable declaration and definition in JavaScript, exploring their nuances, practical implications, and best practices for writing clean, predictable code.
In JavaScript, like many other programming languages, the terms "declaration" and "definition" are often used interchangeably, leading to confusion. However, they represent distinct phases in a variable's lifecycle. Understanding this difference is crucial for grasping concepts like hoisting, scope, and memory allocation, which are fundamental to writing robust and error-free JavaScript.
Variable Declaration: Announcing Existence
A variable declaration is simply the act of introducing a variable's name to the JavaScript engine. It tells the engine, "Hey, I'm going to use a variable with this name." At this stage, no memory is typically allocated for a value, and the variable is often initialized with a special value like undefined
(for var
and let
) or remains uninitialized (for const
).
In JavaScript, declarations are subject to 'hoisting', meaning the declarations themselves (but not their assignments) are moved to the top of their containing scope during compilation. This is a key behavior that differentiates var
from let
and const
.
var myVar; // Declaration using var
let myLet; // Declaration using let
const myConst; // Declaration using const (requires immediate initialization)
Examples of variable declarations in JavaScript.
var
and let
declarations can exist without immediate assignment, const
declarations must be initialized at the time of declaration. Failing to do so for const
will result in a SyntaxError
.Variable Definition: Assigning a Value
A variable definition, on the other hand, is the act of assigning a value to a declared variable. This is where memory is allocated to store the actual data that the variable will hold. A variable can be declared once but defined (assigned a value) multiple times throughout its lifecycle.
When you declare and immediately assign a value, you are performing both declaration and definition in a single statement. This is a common practice and often the most straightforward way to introduce and initialize variables.
let message; // Declaration
message = "Hello World"; // Definition (assignment of a value)
const PI = 3.14159; // Declaration AND Definition in one statement
var counter = 0; // Declaration AND Definition
counter = 1; // Re-definition (re-assignment)
Examples illustrating definition and re-definition of variables.
flowchart TD A[Start] A --> B{Variable Declared?} B -->|No| C[Declare Variable (e.g., `let x;`)] B -->|Yes| D[Variable Exists] C --> E[Variable Initialized (e.g., `undefined`)] D --> F{Value Assigned?} E --> F F -->|No| G[Variable Undefined/Uninitialized] F -->|Yes| H[Define Variable (e.g., `x = 10;`)] H --> I[Variable Holds Value] G --> J[End] I --> J
Flowchart illustrating the lifecycle of a JavaScript variable from declaration to definition.
Key Differences and Practical Implications
The distinction between declaration and definition becomes particularly important when dealing with:
- Hoisting:
var
declarations are hoisted and initialized toundefined
.let
andconst
declarations are also hoisted but remain in a "Temporal Dead Zone" until their actual line of code is executed, preventing access before initialization. - Scope:
var
is function-scoped, whilelet
andconst
are block-scoped. This affects where a variable is declared and where it can be defined or accessed. - Re-declaration and Re-assignment:
var
allows both re-declaration and re-assignment.let
allows re-assignment but not re-declaration within the same scope.const
allows neither re-declaration nor re-assignment after its initial definition.
// Hoisting example with var
console.log(hoistedVar); // Output: undefined (declared but not defined yet)
var hoistedVar = "I am hoisted";
console.log(hoistedVar); // Output: "I am hoisted"
// Temporal Dead Zone with let
// console.log(tdzLet); // ReferenceError: Cannot access 'tdzLet' before initialization
let tdzLet = "I am in TDZ";
// Re-declaration and Re-assignment
var a = 10;
var a = 20; // Allowed (re-declaration and re-assignment)
console.log(a);
let b = 10;
// let b = 20; // SyntaxError: Identifier 'b' has already been declared
b = 20; // Allowed (re-assignment)
console.log(b);
const c = 10;
// const c = 20; // SyntaxError: Identifier 'c' has already been declared
// c = 20; // TypeError: Assignment to constant variable.
console.log(c);
Code examples demonstrating hoisting and re-declaration/re-assignment behaviors.
let
and const
over var
. They provide better control over variable scope and prevent common errors related to hoisting and accidental re-declarations, leading to more predictable code.