Idiomatische moderne JavaScript-verkortingen die omslachtige boilerplate-code vervangen.
Essentiële Verkortingspatronen
Nullish Coalescing (??) & Standaardwaarden
// ?? controleert strikt op null of undefined (behoudt 0 en false)
const port = process.env.PORT ?? 3000;
// || valt terug op de standaardwaarde bij elke falsy waarde (0, "", false, NaN)
const title = customTitle || 'Untitled';
Optional Chaining (?.)
// Veilig navigeren door diep geneste objecten zonder TypeError
const street = user?.address?.street;
const firstTag = user?.tags?.[0];
const result = user?.getProfile?.();
Short-Circuit Uitvoering (&&)
// Voer de functie alleen uit als de voorwaarde waar is
isLoggedIn && showDashboard();
Korte Notatie voor Objecteigenschappen & Methoden
const name = 'Alice';
const age = 30;
// Verkorte notatie voor objectliterals
const user = {
name,
age,
greet() {
return `Hello ${this.name}`;
}
};
Array- & Object-Destructuring
const { name, age, role = 'member' } = user;
const [first, second, ...rest] = [1, 2, 3, 4, 5];