DEV Community

Four tricky JavaScript concepts in one line of code

Per Enström on December 10, 2021

My colleague approached me the other day with a line of JavaScript code he had found on Stack Overflow, and asked how it worked. And I thought it w...
Collapse
 
terstuyahmaksim profile image
Max

This is all very funny and cool, but it's bad code because it's hard to read. Good code - no need to document. One glance at the names of the variables makes it clear what should be there and what to do with it. Immediately you need to read it, and more than once. But overall, yes. Very brief and succinct.

Collapse
 
perenstrom profile image
Per Enström

Absolutely. I think that these kinds of functions can be okay in a contained context so that anything using it will call a method with a well understood name, the implementation details are not important for the calling context.

Collapse
 
mrhiden profile image
Marek Krzyżowski

Actually it is not so unreadable but it is very useful to repack object. I would split only to arrow function pickUp and use that in the next line. This way it is readable and useful. I know there is such a example with pickAC function.

Collapse
 
alex_alex_0417642415a7f05 profile image
Alex Keeprock

The longer you code the more you appreciate simplicity of a code you see. Cause you don't have a time for such trickery - it only matter how much you ship. Code should be «good enough to read by someone else without explanation», not a nomination for a hackathon «hall of fame».

Collapse
 
rtpharry profile image
Matthew Harris

Also a typo with myfunction and myFunction in the later snippets 😉

Interesting refresher of some concepts!

Collapse
 
perenstrom profile image
Per Enström

Thank you! Greatly appreciate these corrections!

Collapse
 
schinta2 profile image
siva sandeep chintamaneni

Might have meant "arrow function" instead of "array function". "To each their own" - can't agree more :)

Collapse
 
perenstrom profile image
Per Enström

Thank you so much! Didn't spend as much time on proof reading this one as I usually do! :)

Collapse
 
ziadab profile image
Ziad Abouelfarah

I can relate

Collapse
 
fredgarcia profile image
Garcia

Did it work with var in place of const ?

Collapse
 
mrhiden profile image
Marek Krzyżowski

Should work, but I would stay with const.

Collapse
 
perenstrom profile image
Per Enström

To be fair, you can write unintelligable code in almost any language. But yes, this line is not that great, thus this article :)

Collapse
 
johnerincon profile image
John Eduar Rincon G.

Thank you for this clean and clear concepts. I am just a beginner in javascript language and your explanation is very useful. I wlll keep it for my future references.

Collapse
 
perenstrom profile image
Per Enström

Glad to hear!

Collapse
 
ifarmgolems profile image
Patrik Jajcay

No one ever will use this :)