- 1. Shuffle Around Values in an Array
- 2. Check if a Date Is on a Weekend or Weekday
- 3. Cut a Number to a Specific Decimal Point
- 4. Convert Temperatures
- 5. Check if Someone's Using Dark Mode
- 6. Get the Average of an Array of Numbers
- 7. Generate a UUID
- 8. Get the Number of Days Between Two Dates
- 9. Generate a Random Number Within a Range
- Frequently Asked Questions
1. Shuffle Around Values in an Array
Keeping with the deck of cards analogy, this is what it would look like if I’d like to shuffle 52 values:
The code should reveal randomly jumbled values in your console output.
2. Check if a Date Is on a Weekend or Weekday
If you want to change that to check whether the day in question falls on a weekday, just reverse the last comparison:
In the following code, I check whether today is on a weekend:
As an alternative to defining separate
isWeekend variables, you can just use the
NOT operand while expressing an
if-then-else statement for either one of the two variables like so:
3. Cut a Number to a Specific Decimal Point
The output you give to your users needs to be as simple as possible and nothing makes your site or app more unappealing than seeing an enormous garbled string of numbers. To cut off less significant decimal points in a number and display it like that to the user, grab this nifty definition:
To test this, I define a numeric value with a ridiculous amount of decimal places and call this quick function to cut it to two decimal places.
The result should be less of an eyesore.
4. Convert Temperatures
For Celsius to Fahrenheit:
And for Fahrenheit to Celsius:
In my town, the temperature is -2 degrees Celsius. Using the Celsius to Fahrenheit conversion one-liner we can find out what this means to someone living in the US:
That expression yields a temperature of 28.4 degrees Fahrenheit.
5. Check if Someone’s Using Dark Mode
Here’s an example of a check using this variable:
6. Get the Average of an Array of Numbers
Here I make a dataset and pump out the average of all its values through the console:
Using this code, your output should be 409.125.
7. Generate a UUID
uuid npm package that provides this functionality out of the box).
In JS, you can pop out a freshly-minted UUID for your users with this one-line expression:
Here, I test the output:
8. Get the Number of Days Between Two Dates
When determining the age of a certain value (like a user’s account), you’ll have to calculate the number of days that have passed since a certain point. Here’s a simple one-liner that does that:
For an example use case, we pretend that we’re assessing the age of a user who created their account on December 28, 2012 and use this declaration to get the number of days between that date and today:
At the time of writing this, that should yield a result of 3298 days.
9. Generate a Random Number Within a Range
Sometimes, it’s not enough to just generate some random number. You might just need to limit the conditions with which it is generated. With this one-liner, you’ll generate numbers within a certain specified range:
Testing this should be easy:
Frequently Asked Questions
1. Why use UUIDs in cookies?
Since cookies are by their very nature intrusive and occupy space on a user’s machine, you should aim to be as efficient and non-invasive as possible when using these instruments to gather data. Using a UUID provides a two-fold benefit. First, it minimizes the space occupied on client-based storage due to the identifier being only 128 bits in length. Secondly, it does depend on any personal data belonging to the user.
2. I get a “window is not defined” error in Node.js when detecting dark mode. What do I do?
3. Is this code universal to all browsers?
Image credit: Profile side view of nice bearded guy by 123RF
Our latest tutorials delivered straight to your inbox