DEV Community

Discussion on: 20 JavaScript One-Liners That Will Help You Code Like a Pro

Collapse
 
moopet profile image
Ben Sinclair

09 Toggle boolean

const toggleBool = (bool) => (bool = !bool);
Enter fullscreen mode Exit fullscreen mode

This could be simplified to

const toggleBool = b => !b;
Enter fullscreen mode Exit fullscreen mode

because you're not actually toggling the original boolean. I'm not sure it offers any benefit beyond using !b in the first place, though.

const uniqueArr = (arr) => [...new Set(arr)];
Enter fullscreen mode Exit fullscreen mode

I think that if you're going to use full words elsewhere, like isArray you should use it here, and make the function uniqueArray. Most of the time, I think if what you're wanting is a set, you should use it as a set. In those cases you probably don't need to convert it back to an array anyway!

To calculate the days between two dates,
we first find the absolute between two dates and then divide it with 86400000 which is equal to milliseconds in a single day

Except... days aren't always 86400 seconds long, and this will fail when times are close to daylight savings, for example. There's a reason date libraries are complicated :)

Collapse
 
ovi profile image
Muhammad Ovi

Woah, man!
This is really helpful, I appreciate your effort <3