Rules Hub
Code Ruleset
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
fullstack ruleP1universalStack: JavaScript
truthinessarrayscorrectness
Don't rely on an array being falsy; check its length
An empty array is truthy, so `if (arr)` is always true; test arr.length (or compare values) to detect emptiness.
PR: vinify-backend · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codetypescript
| 1 | if (results) { |
| 2 | // runs even when results is [] |
| 3 | } |
Explanation (EN)
An empty array is an existing truthy object and passes a presence check.
Objašnjenje (HR)
Good example
New codetypescript
| 1 | if (results.length) { |
| 2 | // only when non-empty |
| 3 | } |
Explanation (EN)
Objašnjenje (HR)
Notes (EN)
Check length when asking whether an existing array contains entries; first distinguish a missing collection if it is nullable.
Exceptions / Tradeoffs (EN)
A truthiness check can legitimately test presence, not contents. Positive length still does not validate element values or dense indexing.