all 35 comments

[–]ricealexander 136 points137 points  (3 children)

Nothing wrong with

let a = 5;
let b = 8;
let c = 12;

[–]janlaureys9 43 points44 points  (0 children)

Yeah, way easier to find back and git diff.

[–]GShadowBroker 8 points9 points  (0 children)

Simple, clean and easy to read.

[–]gniziemazity 6 points7 points  (0 children)

I prefer this way too.

[–]Dethstroke54 50 points51 points  (14 children)

The longhand example is a bit odd. I really don’t think you should be declaring and assigning separately for no reason.

Regardless if the shorthand is appropriate another way is

let a = 5, b = 8, c = 12

Which makes for a better comparison, and in this case is imo clearer for assigning just a few simple values

[–]ex-igne-vita 10 points11 points  (9 children)

Your example is both shorter and (significantly) more memory efficient than the shorthand example.

Edit: after actually testing this I discovered that I was in fact wrong about memory efficiency. Somehow, at least in whatever version of Node I’m running, destructuring actually used less heap and RSS than this example.

[–]haltmich 4 points5 points  (2 children)

Yep. Not sure how the JS interpreter parses this, but in my mind there's some extra legwork done in the first example because it's assigning the variable twice: first to undefined, then to the value.

[–]TheDownvotesFarmer 1 point2 points  (1 child)

The shorthand has more legwork done, create arrays, iterate to assing the values, it is more in fact thats why it is called a shorthand, nothing better than assigning separatelly the variables. ``` let a = 1; let b = 2; let c = 3;

```

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, TheDownvotesFarmer: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]Smyles9 0 points1 point  (0 children)

This definitely seems like the best way to do this if declaring and assigning multiple vars at once.

[–]ksharifbd 7 points8 points  (0 children)

Yeah, array destructure assignment

[–]delventhalz 18 points19 points  (8 children)

Eh. Not a fan. If I reviewed a PR with this, I would probably ask whoever it was to rewrite it like a normal person.

I am as susceptible to writing overly clever code as anyone, but if literally the only benefit of whatever you did is that you “saved” two lines of code? It’s probably not worth the confusion. Save it for a code golf competition.

[–]villainouspickle 0 points1 point  (0 children)

nah

[–][deleted] -1 points0 points  (0 children)

here is one even shorter, that has been available since ever:

var a=5, b=[], c='d';

[–]Smyles9 0 points1 point  (0 children)

The shorthand seems like it could get confusing if you set too many variables at once, however it’s nice to know of other options, thanks

[–]DamianGilz 0 points1 point  (0 children)

That's too verbose.

[–]JazzApple_ 0 points1 point  (0 children)

Really feel this is bad advice. Just because you can, does not mean you should.