| technology | JavaScript | ||||
|---|---|---|---|---|---|
| domain | frontend | ||||
| level | Senior/Architect | ||||
| version | ES2022+ | ||||
| tags |
|
||||
| ai_role | Senior JavaScript Expert | ||||
| last_updated | 2026-04-05 |
Note
Context: Redundancy in conditional logic.
if (isValid === true) { /* ... */ }Comparing a boolean to true or false is redundant. It adds visual noise without increasing safety.
if (isValid) { /* ... */ }
if (!isPending) { /* ... */ }Leverage JavaScript's truthiness/falsiness or direct boolean evaluation. It makes the code more concise and idiomatic.
Note
Context: Object and Array instantiation.
const list = new Array(1, 2, 3);
const map = new Object();The Array constructor is inconsistent: new Array(3) creates an empty array of length 3, while new Array(3, 4) creates [3, 4]. Literals are faster and more readable.
const list = [1, 2, 3];
const map = {};Use literals [] and {}. They are visually cleaner and perform slightly better as they don't involve a function call.
Note
Context: The Single Responsibility Principle (SRP).
function processOrder(order) {
// 100 lines of validation, DB saving, email sending...
}Large functions are hard to test, debug, and reuse. High cyclomatic complexity makes it difficult for the JIT compiler to optimize the function.
function validateOrder(order) { /* ... */ }
function saveToDatabase(order) { /* ... */ }
function notifyUser(order) { /* ... */ }
function processOrder(order) {
validateOrder(order);
saveToDatabase(order);
notifyUser(order);
}Break functions into smaller, pure components. Aim for functions under 20 lines that do exactly one thing.
Note
Context: Cognitive load and code readability.
function getData(user) {
if (user) {
if (user.isActive) {
if (user.hasPermission) {
return fetchData();
}
}
}
}"Arrow code" (code that expands horizontally) is hard to follow. It forces the reader to keep track of multiple nesting levels in their mental stack.
function getData(user) {
if (!user || !user.isActive || !user.hasPermission) {
return null;
}
return fetchData();
}Use "Guard Clauses" to return early. This flattens the structure and handles edge cases first, leaving the happy path at the lowest nesting level.
Note
Context: Self-documenting code.
const d = new Date();
const u = users.map(i => i.n);Single-letter variables (except for standard loop indices like i or j) provide no context. They make the code unsearchable and confusing for other developers.
const today = new Date();
const userNames = users.map(user => user.name);