JavaScript Variables
Variables are the fundamental building blocks of any programming language. They store data that can be used and manipulated throughout your program.
📝 Variable Declaration: var, let, const
JavaScript provides three ways to declare variables, each with different scoping and reassignment rules.
| Keyword | Scope | Reassignable | Redeclarable | Use Case |
|---|---|---|---|---|
var | Function | ✅ Yes | ✅ Yes | Legacy code (avoid in modern JS) |
let | Block | ✅ Yes | ❌ No | Variables that change |
const | Block | ❌ No | ❌ No | Constants that don't change |
⚠️ Modern Best Practice
Use const by default, and only use let when you need to reassign a variable. Avoid var in modern JavaScript code.
💡 Memory Aid:
- const - For constant values
- let - When you need to let the value change
- var - Variable (older way, avoid)
Try It Yourself:
🎯 Variable Scope
Understanding Scope
Scope determines where variables are accessible in your code. JavaScript has:
- Global Scope - Accessible everywhere
- Function Scope - Accessible only within a function (
var) - Block Scope - Accessible only within a block
{}(let,const)
🔍 Scope Examples:
// Global scope
let global = "I'm everywhere";
function test() {
// Function scope (var)
var functionScoped = "I'm in function";
if (true) {
// Block scope (let, const)
let blockScoped = "I'm in block";
}
}⬆️ Variable Hoisting
Hoisting is JavaScript's behavior of moving declarations to the top of their scope before code execution.
✅ var hoisting:
var declarations are hoisted and initialized with undefined
console.log(x); // undefined
var x = 5;❌ let/const hoisting:
let and const are hoisted but not initialized (Temporal Dead Zone)
console.log(x); // Error
let x = 5;📚 Temporal Dead Zone (TDZ)
The time between entering scope and being declared where variables cannot be accessed. This helps catch errors and write more predictable code.
🎯 Best Practice:
Declare variables at the top of their scope to avoid hoisting confusion and TDZ errors.
🏷️ Naming Conventions
JavaScript Naming Rules
- Names can contain letters, digits, underscores, and dollar signs
- Names must begin with a letter, $, or _
- Names are case sensitive (
myVar≠myvar) - Reserved words cannot be used as names
Naming Styles
- camelCase - variables and functions
- PascalCase - classes and constructors
- UPPER_CASE - constants
- kebab-case - HTML attributes and CSS classes
🎯 Pro Tips:
- Use descriptive names (
userAgevsa) - Be consistent with your naming style
- Use verbs for functions (
calculateTotal)
- Use booleans that sound like yes/no questions (
isActive) - Avoid single letter names except in loops
- Make names pronounceable and meaningful
🎮 Practice: User Profile System
Apply what you've learned! This example uses different variable types to create a user profile system. Try modifying the values and see what happens.
💪 Challenges:
- Add a
lastLogindate variable - Create a
userLevelbased on login count - Add more user preferences
- Try to reassign the
USER_IDconstant - Create a function to update user profile
- Add validation for user age
📚 Summary
Key Takeaways:
- Use
constfor values that don't change - Use
letfor variables that need reassignment - Avoid
varin modern JavaScript - Understand scope and hoisting behavior
Best Practices:
- Use descriptive, camelCase names
- Declare variables at the top of their scope
- Initialize variables when declaring them
- Use constants for magic numbers and configuration
Ready to learn about the different types of data these variables can hold?
Next: Data Types →📋 Variables Quick Reference
Declaration:
let variableName = value;const CONSTANT_NAME = value;var oldVariable = value;Reassignment:
variableName = newValue; CONSTANT_NAME = value; Error!Multiple Variables:
let a = 1, b = 2, c = 3;