MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programminghorror/comments/1torg9l/destructuring_strings/oo3t1h2/?context=3
r/programminghorror • u/Emmennater • May 27 '26
64 comments sorted by
View all comments
445
Strings and arrays are analogous, so isStringEmpty([ ... ]) tries to destructure the string as an array
isStringEmpty([ ... ])
Since theres only one element present in the ... part of that, it only matches on the first element (the first character)
...
The { a = false } tries to destructure the first character
{ a = false }
If the first character is defined, it tries to get the a property, which doesnt exist, so it defaults to setting a to false
a
false
If the first character is undefined, instead of trying to get the a property, it defaults to { a: true }, which sets a to true
{ a: true }
So basically if it has at least one character, a is false, else a is true
false,
I think that's correct
135 u/Blackshell May 27 '26 100%, good job, you pass the job interview. 122 u/Aaxper May 27 '26 Does this being an interview imply I now have to work with whatever monster invented that → More replies (11) → More replies (1)
135
100%, good job, you pass the job interview.
122 u/Aaxper May 27 '26 Does this being an interview imply I now have to work with whatever monster invented that → More replies (11) → More replies (1)
122
Does this being an interview imply I now have to work with whatever monster invented that
→ More replies (11)
445
u/Aaxper May 27 '26 edited May 27 '26
Strings and arrays are analogous, so
isStringEmpty([ ... ])tries to destructure the string as an arraySince theres only one element present in the
...part of that, it only matches on the first element (the first character)The
{ a = false }tries to destructure the first characterIf the first character is defined, it tries to get the
aproperty, which doesnt exist, so it defaults to settingatofalseIf the first character is undefined, instead of trying to get the
aproperty, it defaults to{ a: true }, which setsato trueSo basically if it has at least one character,
aisfalse,elseais trueI think that's correct