JavaScript Best Practices
javascript-best-practices
Modern JavaScript best practices and performance optimization guidelines. Use when writing, reviewing, or refactoring JavaScript code to ensure optimal performance, maintainability, and modern patterns.
SKILL.md
Full skill instructions
JavaScript Best Practices
Comprehensive guide for modern JavaScript development covering performance, code quality, and best practices.
When to Apply
Reference these guidelines when:
- Writing new JavaScript code
- Reviewing code for performance or quality issues
- Refactoring existing JavaScript code
- Optimizing application performance
- Ensuring code maintainability
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Performance | CRITICAL | perf- |
| 2 | Async Patterns | HIGH | async- |
| 3 | Error Handling | HIGH | error- |
| 4 | Code Quality | HIGH | quality- |
| 5 | Modern Syntax | MEDIUM | modern- |
| 6 | Arrays | MEDIUM | array- |
Quick Reference
1. Performance (CRITICAL)
perf-avoid-memory-leaks- Always cleanup event listeners, timers, intervals, subscriptions, and observers
2. Async Patterns (HIGH)
async-promise-chains- Prefer async/await over .then()/.catch() chains for better readability
3. Error Handling (HIGH)
error-defensive-programming- Use guard clauses and validate input at system boundarieserror-explicit-validation- Don't hide data errors with optional chaining - validate explicitly
4. Code Quality (HIGH)
quality-strict-equality- Use === instead of == (except for null checks)quality-explicit-boolean-checks- Don't rely on truthy/falsy for non-boolean valuesquality-small-functions- Keep functions focused on one task (5-15 lines ideal)quality-no-magic-numbers- Extract numbers to named constantsquality-immutability- Prefer creating new objects/arrays over mutating existing onesquality-pure-functions- Write functions with no side effects when possiblequality-naming-conventions- Use meaningful, consistent names (camelCase, PascalCase, UPPER_SNAKE_CASE)quality-commenting- Comments explain "why", not "what" - code should be self-documentingquality-avoid-console-log- Use structured logging with levels in production
5. Modern Syntax (MEDIUM)
modern-const-let-no-var- Never use var; prefer const, use let only when reassignment is needed
6. Arrays (MEDIUM)
array-mutation-safety- Avoid in-place mutations (push, sort, reverse) on shared arrays
How to Use
Read individual rule files for detailed explanations and code examples:
rules/perf-avoid-memory-leaks.md
rules/async-promise-chains.md
rules/error-defensive-programming.md
rules/quality-strict-equality.md
Each rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md
