Bayu’s counter: hand over any value, the browser decides whether it is taken. Only 8 values are falsy. An interactive Truthy & Falsy playground: change the…
The subtlest JavaScript bugs often come from here. if (count) fails when count is 0, if (arr) always passes even when the array is empty, and x || fallback overwrites a real 0 or empty text. Learn the 8 falsy values and you never have to guess again.
Only 8 values are falsy; everything else is truthy
if (user.age) fails when the age is 0 0 is falsy. Use if (user.age != null) or if ('age' in user) when 0 is a real value.if (arr) still passes when the array is empty An empty array is truthy. Use if (arr.length) or if (arr.length === 0).arr.filter(Boolean) throws away real values filter(Boolean) drops everything falsy, including a 0 or an empty text that may be real. Use a clear predicate, such as x => x != null.[], then {} Both are taken. An empty container is still an object; check what is inside with .length or Object.keys().length.'0' and 0 The text '0' is taken, the number 0 turned away. Form fields are always text: a classic source of bugs.' ' (a space) and '' (empty) A space is taken, empty is turned away. Use trim() to count spaces as empty.!= null, then hand over 0 Now 0 is taken, because != null only turns away null and undefined.