JS Coding Convention
Modern JavaScript/TypeScript coding conventions — distilled from the Airbnb & Google styles.
Naming
camelCasefor variables and functions:userName,getUser()PascalCasefor classes & components:UserCardUPPER_SNAKEfor constants:MAX_RETRIES- Boolean prefix:
is,has,can—isLoading - Avoid abbreviations:
btn❌ →button✅
Variable & types
💡Prefer
const, use let only when you need to re-assign. Do NOT use var.tsconst items: Item[] = []; // immutable reference let count = 0; // re-assign // var ❌
Function
- Pure functions whenever possible — no side effects.
- Parameters ≤ 3. More than that → wrap them in an object.
- Early return instead of nested if.
js// ❌ function f(user) { if (user) { if (user.active) { return doSomething(user); } } } // ✅ function f(user) { if (!user || !user.active) return; return doSomething(user); }
Object & array
js// Spread instead of Object.assign const merged = { ...a, ...b }; // Destructure when reading const { name, age } = user; // Trailing comma for easy-to-read diffs const arr = [ 'a', 'b', 'c', ];
Async
- Use
async/awaitinstead of long chained.then(). Promise.allto run independent work in parallel.- Always have a try/catch boundary, at least in one place.
Linter & formatter
Set up ESLint (extends next/core-web-vitals) + Prettier. Don’t argue about style in PRs — let the machine decide.