Common ES6 Pitfalls and Improvements: A Leader’s Code Review Rants

The article presents a series of JavaScript ES6 code‑review critiques, explaining why older ES5 patterns are problematic and demonstrating modern ES6 alternatives such as destructuring, spread operators, template literals, optional chaining, async/await, and array methods with clear code examples.

php Courses
php Courses
php Courses
Common ES6 Pitfalls and Improvements: A Leader’s Code Review Rants

During a code‑review meeting a team leader loudly criticized the continued use of ES5 syntax, pointing out that it inflates code size and harms readability, and then offered concise ES6 replacements for each case.

1. Property Access

Instead of extracting each property with separate statements, use object destructuring:

const {a,b,c,d,e} = obj;
const f = a + d;
const g = c + e;

When the variable name differs from the property name, rename during destructuring:

const {a: a1} = obj;
console.log(a1); // 1

Provide a default empty object to avoid errors when the source is null or undefined: const {a,b,c,d,e} = obj || {}; 2. Merging Data

Replace Array.concat and Object.assign with the spread operator and a Set for deduplication:

const c = [...new Set([...a, ...b])]; // [1,2,3,5,6]
const obj = {...obj1, ...obj2}; // {a:1,b:1}

3. String Concatenation

Use template literals with conditional expressions to simplify branching logic:

const result = `${name}${score > 60 ? '的考试成绩及格' : '的考试成绩不及格'}`;

4. Conditional Checks

Replace multiple || comparisons with Array.includes:

if (condition.includes(type)) {
  // ...
}

5. Array Search

Prefer Array.find over filter for a single match to avoid unnecessary iteration: const result = a.find(item => item === 3); 6. Flattening Arrays

Use Object.values and Array.flat(Infinity) to collect all member IDs without manual loops:

let member = Object.values(deps).flat(Infinity);

Note: flat is not supported in IE.

7. Optional Chaining

Replace manual null checks with the optional‑chaining operator: const name = obj?.name; 8. Dynamic Property Names

Directly use computed property syntax without an extra variable: obj[`topic${index}`] = '话题内容'; 9. Empty‑Value Checks

Leverage the nullish coalescing operator to simplify truthy checks:

if ((value ?? '') !== '') {
  // ...
}

10. Asynchronous Functions

Convert promise chains to async/await for clearer flow:

const fn = async () => {
  const res1 = await fn1();
  const res2 = await fn2();
  console.log(res1);
  console.log(res2);
};

For concurrent execution, use Promise.all or Promise.race as needed:

Promise.all([fn1(), fn2()]).then(res => console.log(res)); // [1,2]
Promise.race([fn1(), fn2()]).then(res => console.log(res));

The author invites readers to discuss or refute any of the eleven points in future code‑review sessions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptCode reviewasync/awaitpromiseses6array methodsDestructuringObject Spread
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.