Why is an empty string false but an empty array is not?
P粉710478990
P粉710478990 2023-09-08 14:29:03
0
1
496

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粉710478990
P粉710478990

reply all(1)
P粉309989673

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!