Programming Concepts · Chapter 1 · Foundations
How you name a variable — and whether you reuse it — decides whether your code explains itself later, or forces someone to guess.
bookSHelf · Introduction to Programming Concepts and Methodologies · §1.3 · a self-paced section
Outline — by the end of this section you will be able to
§1.3.1 — the habit that separates beginner code from clear code
A variable name should have a clean, obvious meaning — it should describe the data it stores. Looking at variable names can often tell you whether the code was written by a beginner or by someone with experience.
Context Pause — good naming communicates intent
Good naming is not about following rigid rules — it is about communicating intent. Code is read far more often than it is written, so spending a few extra seconds on a good name saves minutes of confusion later.
Unclear
let x = 29.99;
Clear
let totalPrice = 29.99;
§1.3.1 — the naming rules, named
Definition 1.3.1 — Variable Naming Rules
Good variable names follow these guidelines:
userName, not a comment explaining a bad one.a, b, c — a single letter tells the reader nothing.data and value say nothing about what they hold.currentUser, not currentVisitor.Definition 1.3.1: use human-readable names, avoid bare abbreviations, keep names descriptive but concise, and agree on terms.
Reading a name should answer “what is this?” — without needing a comment to explain it.
§1.3.1 — Worked example
Example 1.3.1 — Good vs. Bad Variable Names
Compare these two snippets. Both do the same thing, but one is much easier to understand.
let a = "John"; let b = 90; let c = a + " scored " + b + "%";
Solution. Names that explain themselves:
let studentName = "John"; let examScore = 90; let result = studentName + " scored " + examScore + "%";
§1.3.1 — Practice
Try It Now 1.3.1
Look at these variable names. Which ones are good? Which ones are bad? Why?
let x = 5; let numberOfStudents = 5; let d = "Monday"; let currentDay = "Monday"; let temp = 98.6; let bodyTemperature = 98.6;
Answer: x, d — bad, no information. numberOfStudents, currentDay, bodyTemperature — good, descriptive. temp — borderline; bodyTemperature says it more clearly.
§1.3.2 — the second habit: don’t relabel the box
Some programmers save a bit of typing by reusing the same variable for different purposes, instead of declaring a new one. Imagine a box labeled “books” that you keep using to store shoes, then dishes, then toys, without changing the label — after a while, nobody knows what is actually inside.
§1.3.2 — the same three values, two variable strategies
Reused (bad)
let item = 42; console.log(item); item = "hello"; console.log(item); item = true; console.log(item);
Separate (good)
let answer = 42; console.log(answer); let greeting = "hello"; console.log(greeting); let isComplete = true; console.log(isComplete);
By the third line, item could be anything. answer, greeting, and isComplete each stay exactly one thing.
Insight Note — reuse costs debugging time, not typing time
Modern JavaScript engines optimize variables away, so extra ones cost nothing at runtime. The real cost of reuse is that you must track what a variable holds at every point in the program — one wrong assumption becomes a bug that is hard to find.
§1.3.2 — Practice
Try It Now 1.3.2
The following code reuses a variable. What is confusing about it? Rewrite it using separate variables with clear names.
let x = 100; console.log(x); x = "Alice"; console.log(x); x = true; console.log(x);
Answer: x holds a number, then a string, then a boolean — by the third line you can’t tell what it represents. Rewrite with maxScore, playerName, isGameOver.
Glossary — terms introduced in this section
Coding convention
A set of guidelines for writing code that is consistent and readable.
Descriptive name
A variable name that clearly communicates what the variable holds.
Variable reuse
The practice of using the same variable for different purposes, which makes code harder to understand and debug.
§1.3 — Conclusions
Name variables so they describe what they hold — human-readable, not abbreviated, descriptive but concise, and matched to your team’s own vocabulary.
Reusing one variable for different kinds of values hides what it means at any given point — the code still runs, but nobody, including future you, can tell what it holds.
Next: §1.4 Programming Paradigms and Languages. Back to start.