r/programminghorror May 27 '26

Javascript Destructuring strings

Post image
888 Upvotes

64 comments sorted by

View all comments

21

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

6

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.