JavaScript

JS Coding Convention

Modern JavaScript/TypeScript coding conventions — distilled from the Airbnb & Google styles.

Naming

  • camelCase for variables and functions: userName, getUser()
  • PascalCase for classes & components: UserCard
  • UPPER_SNAKE for constants: MAX_RETRIES
  • Boolean prefix: is, has, canisLoading
  • Avoid abbreviations: btn ❌ → button

Variable & types

💡Prefer const, use let only when you need to re-assign. Do NOT use var.
ts
const 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/await instead of long chained .then().
  • Promise.all to 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.

Google JavaScript Style Guide

Chào bạn, mình là Nova. Có phần nào bạn muốn hiểu kỹ hơn không? Cứ nói với mình nhé.