Mastering Javascript One-Liners to Look Like a Pro
Javascript one-liners are all about writing succinct, efficient, and elegant pieces o...
For further actions, you may consider blocking this person and/or reporting abuse
There are a lot of great one-liners in here that can be very useful, but we may need to be more careful with array shuffling. For some purposes a simple random shuffle will suffice, but most shuffles have bias in them, and the amount of bias can depend on the JavaScript engine.
The Fisher-Yates shuffle is reliable and unbiased.
For a good visualization of differences, take a look at Mike Bostock's page Will It Shuffle? and try it in different browsers to see the variation.
That's true I considered including the bias, however I thought it might confuse people more.
The Fisher-Yates algorithm is great!
Remember that one-liners are not always better/faster/easier.
Readability goes over being clever.
Especially when a one-liner only passes its parameters to another method like this one
What's wrong with just:
If anything, the
copyToClipboard
should check if the clipboard API is available to use.A guide to writing clean and readable code
Mr. Linxed γ» Feb 23
Hex to rgb can be one-lined too (not sure why you include the alpha channel if you're not doing anything to it though...):
We love one liners but it can be challenging and sometimes confusing, especially for junior developers.
Btw, thanks for sharing this. Keep posting β€οΈπ
I agree that we must keep attention that "one line code" cant suffice. For me a good/pro code portion is a code easy to read/understand/adopt/maintain/extend.
Lenght of the code could be a way de improve the adoption but for ex. a var name or method name or statement complexity of a block.. are impotant too. If a junior is able to quickly adopt and extend your code. Then you're a pro π
Thanks for this great article π
Thanks Brice!
Thank you!
Just have to say that I'm really digging this cover image. It reminds me of synthwave music!
Thanks Michael! It was definitely inspired.
From my perspective one-liners are ok as long as they are readable. Nowadays there are a lot of bundlers, compilers etc. which are able to make it more compact so we don't write code for making computer happy. We do it for other developers who need to spend time to understand it.
One good advice: Be pro instead of look like a pro.
There should be a disclaimer that wanting to look like a pro is not being a pro. Writing these and explaining the benefits and drawbacks is being a pro
Great post!
Thank you!
Nice, but that one is even better:
Title: Top 20 JavaScript One-Liners That Don't Actually Work
πWii π³οΈββ§οΈ γ» Feb 15
Javascript is great
I use this one-liner often when a dataset contains duplicates, comes in handy:
(array) => [...new Set(array)]
I love that!
Very useful!
Thanks!
To capitalize first letter of each word in a string.
Thanks for the contribution Gajanan!
Looking like a pro does not make one a pro. This is yet another in a long line of articles pretending to say something interesting while saying very little at all and adding nothing of its own.
It is surprising how many of this exact same article have been written year-to-date. Itβs just so very boring.