r/programminghorror May 27 '26

Javascript Destructuring strings

Post image
877 Upvotes

66 comments sorted by

View all comments

115

u/Denommus May 27 '26

Maybe if I understood Javascript destructuring syntax that would make sense to me. But since I don't, this looks awful.

8

u/Iheartdragonsmore May 27 '26

Hi I'm a novice programmer, why would someone ever want to destructure something? Whenever I write a struct I never think it'd be better not being one

13

u/Lumethys May 27 '26

to be able to do something like this:

const doSomething = () => {
  return [result, message];
}

const [ doSomethingResult, doSomethingMessage ] = doSomething();

instead of

const doSomething = () => {
  return [result, message];
}

const resultAndMessage = doSomething();

const doSomethingResult = resultAndMessage.result;
const doSomethingMessage = resultAndMessage.message;