search

Home  >  Q&A  >  body text

Why is an empty string false but an empty array is not?

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?

P粉710478990P粉710478990444 days ago567

reply all(1)I'll reply

  • P粉309989673

    P粉3099896732023-09-09 10:04:31

    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.

    reply
    0
  • Cancelreply