Member-only story
10 Awesome JavaScript One-Liners Utilities for Daily Usage
Update cookie values, detect hover effect, and more

As JavaScript is one of the most popular languages, there are a ton of libraries out there. That doesn’t mean that we should become obsessed with constantly adding more to our codebase. We might just need this one utility that we can build ourselves.
Roughly five years ago, the deletion of the popular npm module left-pad
brought momentary chaos to the internet. That was a utility that could be easily be built in one line. In fact, it is available in the ES7
specs.
How can we prevent that from happening? Should we constantly reinvent the wheel?
No, we need to be wise and tidy with our npm dependencies. We need to assess if it’s worth adding a lib for just one utility. Coding it ourselves will give us more control and a better understanding of what is being executed.
In this article, we will see an example of ten powerful JavaScript one-liners utilities that are simple to build and test.
1. Generate a Random Boolean
We know that Math.random
returns a value between 0
and 1
. We can use that knowledge to create a very simple boolean generator.
2. Generate a Random Number
We will take advantage of the Math.random
API once more. By multiplying the result by our max number and rounding it. We then will obtain a number between 0
and max
.
It is simple to extend this method further to accept min and max ranges.