Why both
[] == false
and
![] == false
are true?
Values not on the list of falsy values in JavaScript are called truthy values and include the empty array [] or the empty object {} . This means almost everything evaluates to true in JavaScript — any object and almost all primitive values, everything but the falsy values.
Empty strings are "falsy" which means they are considered false in a Boolean context, so you can just use not string.
Use the Boolean object to convert a value to a boolean in Typescript, e.g. Boolean(someValue) . When used as a function, the Boolean object converts the passed in value to a boolean - true if the value is truthy and false if it's a falsy value. Copied!
arrays are objects, objects are truthy. just ask for array. length, if not zero, it will be truthy. when you explicitly convert to Boolean, the array turns into an empty string first, then the empty string turns into false.
The === operator is your friend. Never use the == operator; it will bite you, as you can see.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With