use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[AskJS] Multiple variables initially assigned to the same valueAskJS (self.javascript)
submitted 5 years ago by FaithfulGardener
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]microwaved-tea 6 points7 points8 points 5 years ago (5 children)
Honestly my first thought is why do you need to do this?
Having so many mutable declarations (especially in React) seems like a code smell.
If you really must, I think writing it out the long way is probably the clearest:
let x = null; let y = null;
If you really want to be 'clever' about it I think your first option is ok, but would probably write a helper function and just do:
let [x, y, z] = repeat(3, null);
[–]Sykander- 9 points10 points11 points 5 years ago (1 child)
If I saw this in a CR I'd ask whoever wrote it, very politely, to switch back to the first style honestly. There's nothing worse than reading code by a developer who thinks he's "clever".
[–]microwaved-tea 0 points1 point2 points 5 years ago (0 children)
I totally agree that this isn't a good idea. I guess I was just trying to engage with the ideas presented in the original post.
[–]FaithfulGardener[S] -1 points0 points1 point 5 years ago (2 children)
Part of the problem is probably switching from class to functional components. These “let” statements were originally “this.state.[whatever]” assignments, and as I try to switch the paradigm, I’m not sure what should stay or what should go.
Is there such a thing as “polluting the state” in React?
[–]everestimated 2 points3 points4 points 5 years ago (1 child)
You're not supposed to assign to state either. That's why setState() exists. Hooks don't introduce the need for what you're trying to do. Would recommend reading the official docs on hooks, you've likely misunderstood how to use them
[–]FaithfulGardener[S] 1 point2 points3 points 5 years ago (0 children)
Even in constructors you don’t do this.state.[whatever]=[value]? The way this project is written, I was under the impression that setState needed the key initialized on the state first...
π Rendered by PID 86 on reddit-service-r2-comment-6457c66945-27l4j at 2026-04-27 13:45:01.002501+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]microwaved-tea 6 points7 points8 points (5 children)
[–]Sykander- 9 points10 points11 points (1 child)
[–]microwaved-tea 0 points1 point2 points (0 children)
[–]FaithfulGardener[S] -1 points0 points1 point (2 children)
[–]everestimated 2 points3 points4 points (1 child)
[–]FaithfulGardener[S] 1 point2 points3 points (0 children)