| name | javascript |
| description | Modern JavaScript (ES2020+) patterns. Trigger: When writing JavaScript code, using ES2020+ features, or refactoring legacy JS. |
| license | Apache 2.0 |
| metadata | {"version":"1.1","type":"language"} |
JavaScript
Modern JS/TS patterns for ES2020+ with async code, safe property access, modern syntax.
When to Use
Use when:
- Writing JavaScript with ES2020+ features
- Refactoring legacy JS to modern syntax
- Implementing async operations (promises, async/await)
- Using destructuring, optional chaining, nullish coalescing
Don't use for TypeScript (typescript skill), React (react skill), or Node.js backend.
Critical Patterns
✅ REQUIRED: ES Module Imports
import { readFileSync, existsSync } from "fs";
import { join, resolve } from "path";
import express from "express";
const fs = require("fs");
async function doWork() {
const fs = await import("fs");
}
✅ REQUIRED: No Dead Code
import { something } from "./lib";
const unused = 42;
function neverCalled() {}
import { needed } from "./lib";
const count = needed();
✅ REQUIRED: const/let, Never var
const API_URL = "https://api.example.com";
let count = 0;
var count = 0;
✅ REQUIRED: async/await for Async Operations
async function fetchData() {
try {
const response = await fetch(url);
return await response.json();
} catch (error) {
console.error(error);
}
}
function fetchData() {
return fetch(url)
.then((res) => res.json())
.catch((error) => console.error(error));
}
✅ REQUIRED: Optional Chaining and Nullish Coalescing
const name = user?.profile?.name ?? "Anonymous";
const result = obj?.method?.();
const port = config.port ?? 3000;
const port = config.port || 3000;
const name = (user && user.profile && user.profile.name) || "Anonymous";
✅ REQUIRED: Explicit Boolean Coercion
const hasData = !!data;
const isValid = Boolean(value);
if (data) { }
✅ REQUIRED: Promise.all for Parallel Operations
const [users, posts, comments] = await Promise.all([
fetchUsers(),
fetchPosts(),
fetchComments(),
]);
const users = await fetchUsers();
const posts = await fetchPosts();
const comments = await fetchComments();
Decision Tree
Importing a module?
→ Named imports: import { x } from 'mod' — never require() in ES modules
Unused import/variable?
→ Delete it — no dead code
Async operation?
→ async/await with try/catch
String concatenation?
→ Template literals: Hello ${name}
Default value?
→ ?? for null/undefined; || for any falsy
Property might not exist?
→ Optional chaining: obj?.prop?.nested
Iterate array?
→ .map(), .filter(), .reduce() — use for...of for early breaks
Copy object/array?
→ Spread: {...obj}, [...arr]
Callback?
→ Arrow function unless this context is needed
Multiple independent awaits?
→ Promise.all() for parallel execution
Example
const fetchData = async (url) => {
try {
const response = await fetch(url);
const data = await response.json();
return data?.results ?? [];
} catch (error) {
console.error("Fetch failed:", error);
return [];
}
};
const { name, age = 18 } = user;
const greeting = `Hello, ${name}!`;
Edge Cases
- Parallel async:
Promise.all() for concurrent execution
- Array holes:
.filter(Boolean) to clean sparse arrays
- Number precision: Use decimal.js for financial calculations
- Equality: Always
=== (never ==)
- this binding: Arrow functions don't bind
this
Checklist
Resources