| description | Vibe coding guidelines and architectural constraints for Modern JavaScript Syntax & FP within the frontend domain. | ||||||
|---|---|---|---|---|---|---|---|
| technology | JavaScript | ||||||
| domain | frontend | ||||||
| level | Senior/Architect | ||||||
| version | ES2024+ | ||||||
| tags |
|
||||||
| ai_role | Senior JavaScript Expert | ||||||
| last_updated | 2026-03-22 |
- Primary Goal: Enforce strict adherence to modern ES6+ syntax and functional programming patterns.
- Target Tooling: Cursor, Windsurf, Antigravity.
- Tech Stack Version: ES2024+
Context: Reducing boilerplate in object creation.
const name = 'Alice';
const user = {
name: name,
age: age
};Redundant repetition of keys and values increases file size and makes the code noisier.
const name = 'Alice';
const user = { name, age };Use Property Shorthand. When the key and variable name match, omit the value.
Context: Handling variable numbers of arguments.
function sum() {
const args = Array.prototype.slice.call(arguments);
return args.reduce((a, b) => a + b);
}The arguments object is not a real array (it lacks methods like map or reduce). It is also incompatible with arrow functions and optimization in some V8 versions.
const sum = (...args) => args.reduce((a, b) => a + b);Use Rest Parameters (...args). They create a real array and are more explicit about the function's intent.
Context: Immutability and array manipulation.
const original = [1, 2, 3];
const copy = [];
for (let i = 0; i < original.length; i++) {
copy.push(original[i]);
}Manual loops for copying are verbose and imperative. They increase the surface area for bugs (off-by-one errors).
const original = [1, 2, 3];
const copy = [...original];Use the Spread Operator (...). It is concise, declarative, and highly optimized by modern engines.
Context: Extracting data from complex objects.
const city = user.location.address.city;
const zip = user.location.address.zip;Repetitive property access is verbose and risks "cannot read property of undefined" errors if any parent object is missing.
const { location: { address: { city, zip } } } = user;Use nested destructuring to extract deeply nested values in a single statement. (Note: Combine with optional chaining if path existence isn't guaranteed).
Context: Handling missing arguments.
function setRole(role) {
role = role || 'guest';
// ...
}Using || for defaults is dangerous if the argument is a "falsy" but valid value (like 0, false, or '').
function setRole(role = 'guest') {
// ...
}Use ES6 Default Parameters. They only apply if the argument is undefined.
Context: Declarative vs Imperative programming.
const double = [];
numbers.forEach(n => {
double.push(n * 2);
});forEach relies on side effects (mutating an outer array). It is less expressive and harder to chain than functional alternatives.
const double = numbers.map(n => n * 2);Use map, filter, and reduce for data transformations. They return new arrays and promote immutability.
Context: State management and predictability.
function updateAge(user) {
user.age = 30; // Mutates original object
return user;
}Mutating objects passed by reference can lead to side effects in other parts of the application that share the same reference, making debugging a nightmare.
const updateAge = (user) => ({ ...user, age: 30 });Treat objects as immutable. Use the spread operator to create copies with updated properties.
Context: Simplifying conditional branching.
switch (action) {
case 'CREATE': return doCreate();
case 'UPDATE': return doUpdate();
default: return doNothing();
}switch statements are verbose, require break to prevent fallthrough bugs, and have a non-standard block scope.
const actions = {
CREATE: doCreate,
UPDATE: doUpdate
};
return (actions[action] || doNothing)();Use an Object Literal (or Map) as a lookup table. It is cleaner, faster, and more extensible.
Context: Safe property access in nested objects.
const street = user && user.address && user.address.street;The "logical AND" chain is verbose and repetitive. It quickly becomes unreadable with deeper nesting.
const street = user?.address?.street;Use Optional Chaining (?.). It short-circuits to undefined if any part of the chain is nullish.
Context: Providing fallback values safely.
const timeout = config.timeout || 5000;If config.timeout is 0, the code will incorrectly fall back to 5000 because 0 is falsy.
const timeout = config.timeout ?? 5000;Use Nullish Coalescing (??). It only falls back if the value is null or undefined, allowing 0, false, and '' to be valid.