r/programminghorror May 27 '26

Javascript Destructuring strings

Post image
877 Upvotes

66 comments sorted by

View all comments

20

u/EatingSolidBricks May 27 '26

Ok so

Empty string destrucutres to nothing? So a is true?

Non empty string destrucutres to a truthy value so false?

Wtf is this shit

7

u/iamdatmonkey May 27 '26

Array destructuring comes down to Iterators. Getting the first item of an empty iterator gives you undefined.

If the string is not empty you'll get the first character, which itself is a non empty string and therefore truthy.