Introduction
Every program needs to store and manipulate data. In JavaScript, variables are containers that hold values, and understanding how they work is the foundation of everything you'll build. Unlike statically typed languages, JavaScript figures out types at runtime—a double-edged sword that offers flexibility but requires discipline.
Key Concepts
Variable: A named storage location for data. Declared using let, const, or the legacy var.
Data Type: The classification of data that tells the interpreter how to handle it. JavaScript has primitives (immutable values) and objects (mutable collections).
Dynamic Typing: Variables can hold any type and can change types during execution.
Real World Context
In production applications, poor variable management leads to bugs that are hard to trace. Using const by default prevents accidental reassignments. Understanding the difference between primitives and objects is critical when passing data to functions—primitives are copied by value, objects by reference.
Deep Dive
Declaration Keywords
Modern JavaScript uses let and const. Avoid var as it has function scope rather than block scope.
javascriptconst API_URL = 'https://api.example.com'; // Cannot be reassigned let retryCount = 0; // Can be reassigned retryCount = 1; // OK // API_URL = 'new-url'; // TypeError!
The 7 Primitive Types
- string - Text data:
"hello" - number - Integers and floats:
42,3.14 - bigint - Arbitrary precision integers:
9007199254740991n - boolean -
trueorfalse - undefined - Variable declared but not assigned
- symbol - Unique identifiers:
Symbol('id') - null - Intentional absence of value
Everything else (arrays, functions, objects) is an Object.
javascripttypeof 'hello'; // 'string' typeof 42; // 'number' typeof true; // 'boolean' typeof undefined; // 'undefined' typeof Symbol(); // 'symbol' typeof null; // 'object' (historical bug!) typeof {}; // 'object' typeof []; // 'object'
Common Pitfalls
- Using
varin loops: Variables declared withvarare hoisted and shared across iterations, causing unexpected closure behavior. - Confusing
nullandundefined: Useundefinedfor uninitialized variables; usenullto explicitly indicate "no value". - Assuming
typeof null === 'null': This returns'object'due to a legacy bug in JavaScript.
Best Practices
- Default to
const: Only useletwhen you need to reassign. Never usevar. - Use meaningful names:
userAgeis better thanx. - Initialize variables: Avoid leaving variables as
undefinedwhen possible. - Use strict equality (
===): Avoids type coercion surprises.
Summary
JavaScript variables are declared with const (immutable binding) or let (mutable binding). The language has 7 primitive types and objects. Understanding the difference between primitives (passed by value) and objects (passed by reference) is fundamental to avoiding bugs.
Code Examples
const API_URL = 'https://api.example.com'; // Cannot be reassigned
let retryCount = 0; // Can be reassigned
retryCount = 1; // OK
// API_URL = 'new-url'; // TypeError!typeof 'hello'; // 'string'
typeof 42; // 'number'
typeof true; // 'boolean'
typeof undefined; // 'undefined'
typeof Symbol(); // 'symbol'
typeof null; // 'object' (historical bug!)
typeof {}; // 'object'
typeof []; // 'object'