Skip to main content
Albero Academy

Modern JavaScript & Best Practices

The ES2020+ features you should use daily — optional chaining, nullish coalescing, destructuring — plus the habits of clean, professional code.

Chapter 16JavaScript15 min readBeginner friendly
  • #JavaScript
  • #ES6+
  • #Best Practices

Modern JavaScript (ES2015 onward) added features that make code shorter and safer. This closing chapter collects the ones you'll reach for every day, then the professional habits that separate readable, maintainable code from the kind that becomes a liability.

Optional chaining & nullish coalescing

?. safely reads deep properties that might not exist (no more "cannot read property of undefined"), and ?? provides a fallback only for null/undefined — unlike ||, which also triggers on 0 and "":

?. and ??
user?.profile?.email // undefined instead of throwing
user?.save?.() // call a method only if it exists
const count = data.count ?? 0; // 0 stays 0 (?? only catches null/undefined)
const count2 = data.count || 0; // ✗ turns a real 0 into 0... but also "" and false

Destructuring & spread

pull apart, spread together
const { name, package: pkg = 0 } = student; // rename + default
const [first, ...rest] = tools; // first item + the rest
const merged = { ...defaults, ...overrides }; // merge objects
const combined = [...a, ...b]; // combine arrays
function log({ level = "info", message }) { // destructure params
console.log(`[${level}] ${message}`);
}

Handy shorthands

small wins
const name = "Aanya";
const obj = { name }; // shorthand for { name: name }
const greet = (n) => `Hi ${n}`; // arrow + template literal
arr.length ? doWork() : skip(); // ternary
const value = input ?? computeDefault();

Clean-code habits

  • Name things well. const activeStudents beats const arr2. Code is read far more than written.
  • Small functions. One job each; if you can't name it simply, it's doing too much.
  • const by default, let only when reassigning, never var.
  • Fail fast. Validate inputs and return early rather than nesting deep ifs.
  • Prefer immutability and pure functions where practical (Chapter 13).
  • Use a linter & formatter (ESLint + Prettier) so style is automatic and consistent.

Practice

Refactor a snippet that uses nested ifs and && guards to use ?. and ??. Destructure a function's options parameter with defaults. Then take a long function and split it into two well-named smaller ones.

Key takeaways

  1. 01Optional chaining ?. safely reads maybe-missing properties; nullish ?? falls back only on null/undefined.
  2. 02Destructuring (with defaults/rename) and spread make working with objects and arrays concise.
  3. 03Use shorthands (property shorthand, arrows, ternary) where they aid readability.
  4. 04Clean-code habits: good names, small single-purpose functions, const-by-default, early returns, immutability.
  5. 05Automate style with ESLint + Prettier — clear beats clever.