Variables in JavaScript - let, const, var Explained
Master variables in JavaScript! Learn when to use let, const, var, scoping, and best practices!
Introduction
Variables are containers for storing data! In modern JS, we have let, const, and the old var! Let's master them!
What You Will Learn
- How to declare variables with let, const, var
- Differences between them
- Scoping rules
- Best practices for choosing which to use
- Common pitfalls
Why This Matters
Variables are the building blocks of any program! Choosing the right one and understanding scoping prevents bugs!
Prerequisites
let: The "Normal" Variable
Use let for variables that will change!
let count = 0;
count = 1; // Perfectly fine!
count++; // 2, still fine!
Characteristics of let
- Block-scoped: Only exists inside
{ ... }it was declared - Can be reassigned: But not re-declared in the same scope!
- Hoisted but not initialized: Can't use before declaration!
const: The "Constant" Variable
Use const for variables that won't change!
const PI = 3.14159;
const name = 'John';
// PI = 3; // ERROR! Can't reassign const!
Important! const with Objects/Arrays
const only prevents reassigning the reference, not the contents!
const person = { name: 'John' };
person.name = 'Jane'; // OK - modifying object, not reassigning variable!
// person = { name: 'Bob' }; // ERROR! Reassigning is not allowed!
const arr = [1, 2, 3];
arr.push(4); // OK - modifying array!
Characteristics of const
- Same scoping rules as let
- Must be initialized when declared!
const x; // ERROR! - Can't be reassigned
var: The Old One (Avoid!)
var is the old way. Avoid in modern code! Use let/const instead!
Problems with var
- Function-scoped, not block-scoped! Weird behavior!
- Hoisted and initialized to undefined
- Can be re-declared in same scope!
for (var i = 0; i < 3; i++) {
console.log(i); // 0,1,2
}
console.log(i); // 3! i exists outside loop - BAD!
Compare to let:
for (let i = 0; i < 3; i++) {
console.log(i);
}
// console.log(i); // Error! i doesn't exist here! Good!
Scoping Rules
Global Scope
Variables declared outside any function/block are global! Avoid too many globals!
Function Scope
Variables declared inside function only exist there!
Block Scope (let/const)
Variables declared inside { } only exist there!
if (true) {
let a = 1;
const b = 2;
var c = 3;
}
console.log(a); // Error!
console.log(b); // Error!
console.log(c); // 3 (var leaks!)
Hoisting
Variables are "hoisted" to top of their scope!
var: Hoisted and initialized toundefinedlet/const: Hoisted but NOT initialized → "Temporal Dead Zone" (TDZ)
console.log(x); // undefined (var - hoisted, undefined value)
var x = 5;
// console.log(y); // Error! TDZ!
let y = 5;
Best Practices
- Use
constby default! Most variables don't need to change! - Use
letonly when you need to reassign - Never use
varin modern code! - Use descriptive variable names:
usernamenot justu! - Use camelCase:
firstName,totalPrice
Common Mistakes
- Using var: Just don't!
- Forgetting const needs initial value:
const x; // Error! - Trying to reassign const:
const a = 1; a = 2; // Error! - Confusing block vs function scope!
Code Examples
Good
// Good! Use const for things that don't change
const PI = 3.14159;
const baseURL = 'https://api.example.com';
// Good! Use let for things that do change
let score = 0;
let username = '';
function greet(name) {
const message = `Hello, ${name}!`; // const inside function!
console.log(message);
}
Bad
// Bad! Use const instead!
var daysInWeek = 7;
// Bad! Use const instead!
let taxRate = 0.08; // Never changes - use const!
Summary
Use const by default, let when needed, and never var! This will save you from countless bugs!
Related Articles
Previous Tutorial
Next Tutorial
Let's learn data types! → Data Types: Primitive & Reference