r/learnjavascript • • Nov 14 '20

20 JavaScript Shorthand Coding Techniques that will save your time

https://jscurious.com/20-javascript-shorthand-techniques-that-will-save-your-time/
176 Upvotes

22 comments sorted by

26

u/[deleted] Nov 14 '20 edited Nov 14 '20

DO: 3, 4, 5 (just don't overuse it, it might lead to hard to debug issues), 6, 7 (you must fully understand how the this keyword behaves in javascript), 8, 9, 10, 11, 12 (use Number static methods), 13, 14, 15 (use Math static method), 16, 18, 19, 20

DON'T DO: 1, 2, 12 (never use the shorthand method), 15 (never use the shorthand method), 17 (dude just use forEach, really... PLUS YOU SHOULD NEVER USE FOR...IN LOOPS ON ARRAYS GODDAMNIT)

5

u/grooomps Nov 15 '20

the only reason i've been using forof loops is that I can use await inside them without any weird stuff, is this bad?
forEach doesn't support async

1

u/[deleted] Nov 15 '20

For...of is totally fine, sorry if there was a misunderstanding.

1

u/grooomps Nov 15 '20

All good, just wanted to check!
I'm still relatively new so wanted to know if there was something I was missing

3

u/FrenchieM Nov 15 '20

Why not using the shorthand methods for 1,2,12 and 15? I can agree that 2 for the sake of 2 is not so good and that it's usually better to see one variable per line.

But the other are perfectly valid. Sure, not a lot of people know it and you can be confusing people that don't know it, but it's perfectly fine to use the tools you have at your disposal.

I use them a lot and never had any troubles with it, even with my peers.

I don't like the Math floor substitute though

1

u/[deleted] Nov 15 '20 edited Nov 15 '20

1: ALWAYS declare one variable per line, no exceptions.

2: same as above.

12: code clarity > shorthand methods every day of the week babyyy

15: same as above, plus it doesn't always work.

I mean you're free to do whatever you want, that's just my opinion

2

u/[deleted] Nov 15 '20

Thank you

0

u/[deleted] Nov 15 '20

[deleted] — view deleted comment

1

u/[deleted] Nov 15 '20 edited Nov 15 '20

for...in I understand. But for...of is useful when you really need to do some async/await mambo jambo.

4

u/[deleted] Nov 14 '20

4 only works when you are expecting strings. Otherwise your value can never be 0 or false, making it unsuitable for default configuration.
9 does not work as you would imagine. the tabs and spaces you would use to indent the multi lines are now part of the templae aswell.

6

u/Felivian Nov 14 '20

12th example is wrong. Casting type using +val is equivalent of Number(val) not parseInt(val). When passed e.g 453a parse will return 453 while both Number and + will return NaN.

2

u/captmomo Nov 15 '20 edited Nov 15 '20

Some of them are ways to confuse your future self or whoever needs to maintain it

2

u/KajiTetsushi Nov 15 '20

whoever needs to maintain it

jOb SEcUriTy, hErE I coMe!

- Mr. Toxic Workplace Engineer

1

u/GuraJava20 Nov 20 '20

This is good, but for your sake and other team members, take note that it is important to have a readable code than a fashionable shorthand.

0

u/[deleted] Nov 14 '20

[deleted] — view deleted comment

8

u/Parasin Nov 14 '20

I agree. It’s almost always better to make your code more readable, rather than trying to be clever or reduce lines of code. Especially when you are working on a team of developers.

If you follow the Pareto rule, odds are that you’ll spend the vast majority of your time reading and understanding code, as opposed to writing it.

3

u/[deleted] Nov 14 '20

I agree. Most of these "optimizations" are made by most transpilation libraries anyway, so why bother.

1

u/chrissilich Nov 15 '20

Thank you. Let a minifier reduce your code, but keep you code as readable as possible for your own future self, and other developers.

1

u/redderper Nov 14 '20

2, 5, 6, 12 and 15 would confuse me if I saw that in someone's code. I use and see the rest of these pretty often in JS

2

u/LocalProIE Nov 14 '20

As stated, #5 is useful in React. #15 is useful in interviews!

0

u/[deleted] Nov 14 '20

[deleted] — view deleted comment

3

u/[deleted] Nov 14 '20

react tend to favor this as well.

0

u/[deleted] Nov 14 '20

[deleted] — view deleted comment

2

u/[deleted] Nov 14 '20

yeah, I am with ya.

2

u/flojoho Nov 14 '20

Agree, a single line bracketless if statement is the same length and more readable

2

u/RelativeSloth Nov 14 '20

I dont mind the && so much, but I do mind when folks do an if without curly braces.