JavaScript
The JavaScript skill provides expertise in modern JavaScript (ES6+) for Node.js and browser environments.
When Activated
Section titled “When Activated”- Working with JavaScript files (
.js,.mjs) - Browser scripting
- Node.js applications without TypeScript
Core Patterns
Section titled “Core Patterns”Modern Syntax
Section titled “Modern Syntax”// Destructuringconst { name, email } = user;const [first, ...rest] = items;
// Spread operatorconst merged = { ...defaults, ...options };const combined = [...array1, ...array2];
// Template literalsconst message = `Hello, ${name}!`;
// Optional chaining and nullish coalescingconst city = user?.address?.city ?? 'Unknown';Async Patterns
Section titled “Async Patterns”// Async/awaitasync function fetchData(url) { const response = await fetch(url); if (!response.ok) throw new Error('Fetch failed'); return response.json();}
// Promise.all for parallelconst results = await Promise.all([ fetchData(url1), fetchData(url2),]);
// Error handlingtry { const data = await fetchData(url);} catch (error) { console.error('Failed:', error.message);}Array Methods
Section titled “Array Methods”// Map, filter, reduceconst names = users.map(u => u.name);const active = users.filter(u => u.active);const total = items.reduce((sum, i) => sum + i.price, 0);
// Find and includesconst user = users.find(u => u.id === id);const hasAdmin = users.some(u => u.role === 'admin');Classes
Section titled “Classes”class UserService { #db; // Private field
constructor(database) { this.#db = database; }
async findById(id) { return this.#db.users.find(u => u.id === id); }}Best Practices
Section titled “Best Practices”- Use
constby default,letwhen needed - Avoid
var- use block-scoped declarations - Use arrow functions for callbacks
- Handle all promise rejections
- Use ESLint for consistent style
Common Pitfalls
Section titled “Common Pitfalls”Implicit Type Coercion
Section titled “Implicit Type Coercion”// ❌ BAD: Uses ==if (value == null) { }
// ✅ GOOD: Uses ===if (value === null || value === undefined) { }// Or better:if (value == null) { } // Only for null/undefined checkCallback Hell
Section titled “Callback Hell”// ❌ BAD: Nested callbacksgetData(function(a) { getMoreData(a, function(b) { getMoreData(b, function(c) { // ... }); });});
// ✅ GOOD: Async/awaitconst a = await getData();const b = await getMoreData(a);const c = await getMoreData(b);Mutating Objects
Section titled “Mutating Objects”// ❌ BAD: Mutates originalfunction addProperty(obj) { obj.newProp = 'value'; return obj;}
// ✅ GOOD: Creates new objectfunction addProperty(obj) { return { ...obj, newProp: 'value' };}Not Handling Errors
Section titled “Not Handling Errors”// ❌ BAD: Unhandled rejectionasync function loadData() { const data = await fetch(url); return data.json();}
// ✅ GOOD: Handle errorsasync function loadData() { try { const response = await fetch(url); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } return response.json(); } catch (error) { console.error('Load failed:', error); throw error; }}Related Skills
Section titled “Related Skills”- TypeScript - Typed JavaScript
- React - UI components
- Next.js - Full-stack framework