Rules Hub

Coding Rules Library

← Back to all rules
fullstack ruleStack: general
javascripttypescriptclean-codesyntaxes6

Prefer logical AND for conditional object properties

Use logical AND (&&) instead of the ternary operator when conditionally spreading properties into an object to reduce verbosity.

PR: Feat/FCK-2204 - Send date of birth to MC on payment init #715Created: Dec 7, 2025

Bad example

Old codets
1const requestBody = {
2 id: user.id,
3 ...(phone ? { phoneNumber: phone } : {}),
4 ...(email ? { emailAddress: email } : {})
5};

Explanation (EN)

The ternary syntax requires explicitly returning an empty object ({}) for the false case. This adds unnecessary visual noise and verbosity for a simple conditional inclusion.

Objašnjenje (HR)

Ternarna sintaksa zahtijeva eksplicitno vraćanje praznog objekta ({}) za slučaj kada uvjet nije zadovoljen. To dodaje nepotreban vizualni šum i opširnost za jednostavno uvjetno uključivanje.

Good example

New codets
1const requestBody = {
2 id: user.id,
3 ...(phone && { phoneNumber: phone }),
4 ...(email && { emailAddress: email })
5};

Explanation (EN)

Using the logical AND operator (&&) is cleaner. Spreading a falsy value (like null, undefined, or false) inside an object is valid in modern JavaScript and results in no operation, avoiding the need for the empty object fallback.

Objašnjenje (HR)

Korištenje logičkog AND operatora (&&) je čišće. Širenje (spread) falsy vrijednosti (poput null, undefined ili false) unutar objekta validno je u modernom JavaScriptu i ne radi ništa, čime se izbjegava potreba za praznim objektom.