JavaScript Variables and Data Storage: Beginner Guide to var, let, and const
How to Declare, Name, and Store Data in Modern JavaScript Without Coercion Bugs
Quick Answer:
Variables in JavaScript are named containers used to store data values. Modern JavaScript usesconstby default for values that never change andletfor values you intend to reassign. Avoid using the legacyvarkeyword and never create undeclared variables automatically. Identifiers are case-sensitive, and mixing strings with numbers during addition triggers automatic type concatenation.
1. What Is a Variable? (Containers for Data)
Think of a variable as a labeled storage box inside your computer's memory. When you assign a value to a variable, you place an item into that box so you can retrieve or update it later in your program.
JavaScript provides 4 ways to declare a variable:
- Automatically (Undeclared) — Never use this in real projects.
- Using
var— Legacy syntax (1995–2015). - Using
let— Modern syntax for reassignable values. - Using
const— Modern syntax for permanent values.
Practical Rule of Thumb:
- Default to
constfor every variable, object, and array you create. - Switch to
letonly when you know the value must change (such as counters in loops or running totals). - Never use
varunless maintaining an ancient legacy codebase without modern build tools.
2. The Assignment Operator (=) vs. Algebra
In mathematics, the equal sign (=) denotes equivalence ($x = y$). In JavaScript, the single equal sign is the assignment operator. It evaluates whatever is on the right side and assigns the resulting value to the variable on the left side:
The computer first computes score + 5 (which equals 15), then places that new value back into the score storage container.
3. Declaring vs. Initializing: The undefined State
Creating a variable name is called declaring it. Giving it a starting value is called initializing it:
Declaring Multiple Variables in One Statement
You can declare several variables in a single statement by separating them with commas:
4. Variable Naming Rules (Identifiers)
Variable names are called identifiers. JavaScript enforces specific naming constraints that every developer must follow:
- Allowed Characters: Names can contain letters, numbers, underscores (
_), and dollar signs ($). - Start Character: Names must begin with a letter,
$, or_. They cannot start with a number:
Case Sensitivity: Identifiers are strictly case-sensitive. lastName and lastname are treated as two distinct storage containers:
- No Reserved Keywords: You cannot use JavaScript keywords (such as
let,class,return, orfunction) as variable names.
Special Identifiers: The Dollar Sign ($) and Underscore (_)
JavaScript treats $ and _ just like regular letters:
5. Variable Re-declaration: Why let and const Prevent Bugs
One of the biggest flaws with legacy var is that it allows accidental re-declaration without raising any errors, silently overwriting existing variables:
Modern JavaScript solves this hazard. Re-declaring a variable defined with let or const within the same scope immediately triggers an explicit error:
6. Arithmetic vs. String Concatenation Quirks
JavaScript handles arithmetic dynamically based on data types. The plus operator (+) serves a dual role: it performs mathematical addition for numbers and text concatenation for strings.
When you mix numbers and strings with +, JavaScript converts the numbers into strings:
Quick Comparison: var vs. let vs. const
| Feature | var (Legacy) | let (Modern) | const (Modern) |
|---|---|---|---|
| Scope | Function scope | Block scope {} | Block scope {} |
| Reassignable? | Yes | Yes | No |
| Can Re-declare? | Yes (Dangerous) | No (Protected) | No (Protected) |
| Default Value | undefined | undefined | Must initialize upon declaration |
| Best Use Case | Avoid in new code | Counters, dynamic states | Default choice for all variables |
Summary Checklist
- Always declare variables explicitly using
constorlet. - Use
constby default; only switch toletwhen the stored value must be reassigned. - Remember that variable names are case-sensitive and cannot begin with digits.
- Beware of string concatenation when adding numbers with the
+operator.
First Lesson
You are at the beginning of this curriculum.
Latest Tutorial
More chapters coming soon to this topic.
Leave a Reply
Share your insights, questions, or solutions with the developer community.

Discussion
0No comments yet
Be the first to share your thoughts, question a concept, or provide additional tips!