DEV Community

Cover image for Best JavaScript One Liners that You Must Know

Best JavaScript One Liners that You Must Know

Shubh Sharma on May 21, 2024

Introduction JavaScript offers powerful techniques that can be used to to do very complex tasks in one line, these can be called one-lin...
Collapse
 
sreno77 profile image
Scott Reno

Please never use chaining in ternary operators because it's hard to follow and can lead to errors.

Collapse
 
anotherguy profile image
Igor Soloydenko

They are not more difficult to read than the 8f-else-if chains

Collapse
 
fastndead profile image
Umbetov Asman

Right off the bat the nested ternaries should be considered a crime

Collapse
 
anotherguy profile image
Igor Soloydenko

No, they shouldn't

Collapse
 
shubhsharma19 profile image
Shubh Sharma

Totally agree but its still a valid one liner in js :)

Collapse
 
joo_carlosrovedaostrov profile image
João Carlos Roveda Ostrovski

Can you elaborate please, i'm new to programming, and would love to know why.

Thank you.

Collapse
 
alexbodn profile image
abraham

the danger is precedence order of the operations in the ternary operator expression.
expressions should be put in parantheses.

Thread Thread
 
joo_carlosrovedaostrov profile image
João Carlos Roveda Ostrovski

Thank you!

Collapse
 
smits profile image
SmitS

might have to update your article. swapping values with const won't work... this could be misleading for someone starting fresh.

also I don't agree with the use of chaining ternary operator like others. but it could be just my perspective.

anyways, good luck!

Collapse
 
sthasam2 profile image
SthaSAM

That ternary chaining is absolutely horrible for readability.

Collapse
 
shubhsharma19 profile image
Shubh Sharma

Done Thanks a lot!

Collapse
 
debu0534 profile image
Debjyoti Banerjee • Edited

Swapping values using destructuring with const

🫣🥶

Collapse
 
syeo66 profile image
Red Ochsenbein (he/him)
const a = 10;
const b = 20;
[a, b] = [b, a];
Enter fullscreen mode Exit fullscreen mode

Did you even test that?

Collapse
 
itsjp profile image
Jaydeep Pipaliya

yes it works but the only thing that needs to change is let instead of const

Collapse
 
syeo66 profile image
Red Ochsenbein (he/him)

That's my point... Not tested. Maybe even AI generated....

Collapse
 
emmanuelibikunle profile image
Emmanuel Ibikunle

No it doesn't
It would return a SyntaxError because a is already declared.

Collapse
 
shubhsharma19 profile image
Shubh Sharma • Edited

That was a mistake since i mostly use const while creating variables so yeah it was a habit of using const there, fixing it now, Thanks!

Collapse
 
alexbodn profile image
abraham

I don't that use const as much as I should :).
and prefer dynamic typing, for creativity.

Collapse
 
sthasam2 profile image
SthaSAM

If the dude doesn't know the difference between let and const, it's better to forgo the whole article, sadly.

Collapse
 
shubhsharma19 profile image
Shubh Sharma

It was a mistake! Fixing it now :)

Collapse
 
davvolun profile image
Dav • Edited

If you're a fresh coder right out of college or maybe a deep back-end dev who is just starting to mess with front end, and you ever give me a nested ternary like that in a pull request, I will come to your place, wherever you live, off-shore, domestic, doesn't matter, and I will punch you directly in the mouth.

Collapse
 
shubhsharma19 profile image
Shubh Sharma

in the face*

Collapse
 
alexbodn profile image
abraham

thank you Shubh, my friend.
your list is reconforting, and definitely good to know :).
about the chained ternary operations, precedence should be taken care of.

Collapse
 
designnotdrum profile image
Nick M.

You have to be trolling with the chained ternaries…. right? No one writes code like this in the real world.

Collapse
 
shubhsharma19 profile image
Shubh Sharma

I have seen that happen in react code while rendering multiple possible jsx code