I know the answer to this question is It's in the spec , but what is the logic behind it (if any)?
Welcome to Node.js v19.0.0. Type ".help" for more information. > !!'' false > !![] true
String
, as I understand it, is actually an array with some extra functionality added to make the text easier to work with, but it can still do all the array-like operations, so why is an Empty string falsey
but empty array is truthy
?
Let’s keep it simple:
The empty string in JavaScript is treated as a "false" value. When evaluated in a Boolean context, it is treated as Boolean false.
Arrays, whether or not they contain items, are considered "real" values in Javascript. When evaluated in a Boolean context, they are treated as Boolean true.