DEV Community

Discussion on: JavaScript shorthand tips and tricks

Collapse
 
moopet profile image
Ben Sinclair

I have a few concerns about some of these.

This pattern:

let [a, b, c] = [5, 8, 12];
Enter fullscreen mode Exit fullscreen mode

is quite difficult to read, because in cases where you have proper variable names you have to dart your eyes left and right and count the position of each thing to know what's being assinged to what.

It also makes version control more difficult because if these were each on their own line then changes would be easier to spot.

Short circuits are popular in REPLs or shell scripts:

isLoggedin && goToHomepage();
Enter fullscreen mode Exit fullscreen mode

... but they make things harder to read in anything but trivial cases and it's easier to make mistakes. @mcsee has a good post about it -

I'm all for multiple value checking, but examples like this:

if ([1, 'one', 2, 'two'].includes(value)) { 
Enter fullscreen mode Exit fullscreen mode

imply that value could be different types, and that's usually a code smell in itself.

Converting to numbers with parseInt needs a second argument, the base that's going to be used. Otherwise you might get some unexpected results. Your shorthand version is better in that case, but using values like +'23' as a kind of cast is going to make the next person to look at the code wonder if you left something out.

let total = parseInt('453'); // No
let total = parseInt('453', 10);  // Yes
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Most of these are concerns due to what you consider to be readable, and readability is purely subjective.