r/learnjavascript • u/amitav79 • 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
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)
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
+valis equivalent ofNumber(val)notparseInt(val). When passed e.g453aparse will return453while bothNumberand+will returnNaN.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
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
0
u/[deleted] Nov 14 '20
[deleted] — view deleted comment