JavaScript Variables: var, let, const & More
What is a Variable?
A variable is a named container for storing data in memory, which can be accessed and modified later. It acts as a reference to a value.
var a = 10; // value 10 is stored in a
Why variables are used?
- Store Data : Hold values like numbers, strings, objects, etc
- Reusability : Use the same value multiple times.
- Dynamic Manipulation : Change values during program execution.
Variable declaration
There are generally 4 ways to decalre a variable
- var keyword. example : var num = 7;
- let keyword. example : let num = 7;
- const keyword. example : const num = 7;
- without_any_keyword. example : num = 7;
var keyword
var keyword can be used to declare or initialize a variable. Variable created with var keyword are
- Function-scoped : Limited to the function in which it is declaredCode javascript
function test() { var c = 10; console.log(c); // Output: 10 } console.log(c); // ReferenceError (c is function-scoped)
- Hoisted with undefinedCode javascript
console.log(a) // Hoisted with undefined Output: undefined var a // variable declaration using var a = 2 // initializing value
- Can be reassigned & redeclared Code javascript
var a // variable declaration using var a = 2 // initializing value console.log(a) //Output: 2 var b = 7 // variable declaration & initialization var b = 3 // redeclaration+initialization b = 9 // reinitialization console.log(b) // Output: 9
let keyword
let keyword can be used to declare or initialize a variable within a block scope. Variable created with let keyword are
- Hoisted but not initialized (in Temporal Dead Zone)
- TDZ is the phase between variable creation and initialization where accessing the variable results in a ReferenceError. This applies to let and const but not var. Code javascript
console.log(a) // TDZ Output: ReferenceError: a is not defined let a // variable declaration using let
- During memory allocation (hoisting phase), let and const variables are placed in a separate "uninitialized" memory space.
- If accessed before initialization, JavaScript throws a ReferenceError.
- TDZ is the phase between variable creation and initialization where accessing the variable results in a ReferenceError. This applies to let and const but not var.
- can be reassigned but cann't redeclaredCode javascript
let a // variable declaration using let a = 2 // initializing value a = 5 // reinitializing value console.log(a) //Output: 5 let b = 7 // variable declaration & initialization let b = 3 // Error : The symbol "b" has already been declared
const keyword
It is used to create constant(Immutable) variables.It is same as let but must be initialized and cannot be reassigned.
const a = 5 // assignment
console.log(a) // Output: 5
a=8 // Error: Cannot assign to "a" because it is a constant
const a = 7 // Error: The symbol "a" has already been declared
Variable without any keywords
It is strongly recommonded to use a keyword while creating a variable, but if you donn't use any then JavaScript implicitly declares the variable in the global scope (even inside functions).
function test() {
x = 7; // varable initialization
}
test();
console.log(x); // Output: 7 (x becomes a global variable)
It doesn't get hoisted (assigned at runtime). In strict mode it throws ReferenceError
Best Practices for Variables
- Use const by default unless you need to reassign the variable.
- Use let if reassignment is needed but avoid var.
- Avoid using variables without declaration keywords to prevent accidental global scope leaks.