Falsy and Truthy in JavaScript
Falsy and Truthy in JavaScript
Falsy
Falsy values are values that are considered false
when encountered in a boolean context. That means values that become false
if you try to convert them to a boolean
.
Boolean('');
//=> false
Boolean(0);
//=> false
Boolean(null);
//=> false
TypeScriptBoolean('');
//=> false
Boolean(0);
//=> false
Boolean(null);
//=> false
The list grows over time, but currently, those are the falsy values in JavaScript:
false
0
-0
0n
representations of zero- ````
""
''
empty string null
undefined
NaN
not a numberdocument.all
possibly
**document.all**
Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot.
That slot only exists in 'document.all' and cannot be set using JavaScript.
markdown**document.all**
Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot.
That slot only exists in 'document.all' and cannot be set using JavaScript.
Truthy
Truthy values are the opposite. They are values that are considered true
when encountered in a boolean context.
Boolean('abc');
//=> true
Boolean(1);
//=> true
Boolean([]);
//=> true
TypeScriptBoolean('abc');
//=> true
Boolean(1);
//=> true
Boolean([]);
//=> true
All values that are not falsy, are truthy.
Conclusion
References are in the references.
We release web development tutorials every two weeks. Consider
Have a great day, and I'll see you soon!