avatarKesk -*-

Summary

This web page provides 30 useful JavaScript one-liners for developers, covering topics such as dates, strings, numbers, arrays, and utilities.

Abstract

The web page titled "30 Super Useful JavaScript One-Liners" offers a collection of JavaScript one-liners that can help developers save time and solve problems more efficiently. The one-liners are grouped into five categories: dates, strings, numbers, arrays, and utilities. Each one-liner is presented with a brief explanation, code snippet, and example. The one-liners cover a range of tasks, such as checking if a date falls on a weekend, converting a letter to its associated emoji, calculating the factorial of a number, and generating a 128-bit UUID. The author encourages readers to use one-liners as a compact and elegant way to solve problems, while also cautioning against making them too difficult to read.

Bullet points

  • The web page provides 30 JavaScript one-liners for developers.
  • The one-liners are grouped into five categories: dates, strings, numbers, arrays, and utilities.
  • Each one-liner is presented with a brief explanation, code snippet, and example.
  • The one-liners cover a range of tasks, such as checking if a date falls on a weekend, converting a letter to its associated emoji, calculating the factorial of a number, and generating a 128-bit UUID.
  • The author encourages readers to use one-liners as a compact and elegant way to solve problems, while also cautioning against making them too difficult to read.

30 Super Useful JavaScript One-Liners

Save time in your day-to-day work and have fun with them

Photo by MART PRODUCTION from Pexels: https://www.pexels.com/photo/close-up-shot-of-a-person-using-a-laptop-7172094/

In this post, I list a series of 30 one-liners in JavaScript that are very useful when developing using vanilla js ( ≥ ES6). They are also an elegant way to solve problems using all the power the language offers us in the latest versions.

I have grouped them into the following categories:

  • Date
  • Strings
  • Numbers
  • Arrays
  • Utils

Without further ado, I’ll get to it. I hope you find them helpful!

Dates

1. Knowing if a date corresponds to the current date

As simple as converting the two dates to the same format and comparing them.

is a Date instance.

isCurrentDay function.
isCurrentDay example

2. How to know if a date is between two dates

We check if the past date is within the min-max range.

, , and are Date instances.

isBetweenTwoDates function.
isBetweenTwoDates example.

3. How to know if a date falls on a weekend

The getDay method returns a number between 0 and 6, representing the day of the week for the given date.

is a Date instance.

isWeekend function.
isWeekend example.

4. Check if a date is in a year

Similar to when we used to check if a date corresponded to the current day. In this case, we get the year and compare it.

and are two Date instances.

isInAYear function.
isInAYear example.

5. Convert an hour to AM-PM format

We can use mathematical expressions to determine whether the elapsed time is less than 13 hours or more and thus determine whether it is “AM” or “PM.”

toAMPMFormat function.
toAMPMFormat example.

Strings

6. Capitalize the first letter of a sentence

We convert the first letter to capital letters and then append the rest of the letters of the sentence using

capitalize function.
capitalize example.

7. Convert a letter to his associate emoji

letterToEmaji function.
letterToEmaji example.

8. How to know if a string is a palindrome

isPalindrome function.
isPalindrome example.

Numbers

9. How to calculate the factorial of a number

getFactorial function.
getFactorial example.

10. How to obtain the Fibonacci of a number

getFibonnaci function.
getFibonnaci example.

11. How to obtain the factorial of a number

getFActorial function.
getFibonnaci example.

Arrays

12. Copy an array to another array

copyToArray function.
copyToArray example.

13. Get the unique elements from an Array

getUnique function.
getUnique example.

14. Shuffle Array

The following snippet shuffles an array in a very efficient way.

shuffle function.
shuffle example.

15. Group an Array by a property

groupBy function.
groupBy example.

16. Reverse a String

We can utilize the built-in Array methods like reverse() and join() to create a one-liner that does the same thing.

reverseString function.
reverseString example.

17. Check if two arrays contain the same values

We can use Array.sort() and Array.join() methods to check if two arrays contain the same values.

containgSameValues function.
containgSameValues example.

Utils

18. Convert to Fahrenheit

toFahrenheit function.
toFahrenheit example.

19 . Convert to Celsius

ToCelsius function.
ToCelsius example.

20. How to clear all cookies from the browser

clearAllCookies function.

21. How to convert from HEX to RGB

toRGB function.
toRGB example.

22. How to convert from RGB to HEX

toHEX function.
toHEX example.

23. Check if a function is an Async function

isAsyncFunction function.
isAsyncFunction example.

24. How to know if a code is running in the browser

runningInBrowser function.

25. How to know if a code is running in node

runningInNode function.
runningInNode example.

26. Detect Dark Mode

This a very handy way to check if the user has dark mode enabled on their browser.

isDarkMode function.
isDarkMode example.

27. Scroll to the Top

A one-line way to scroll elements is to use the method.

toTop function.

28. Scroll to Bottom

toBottom function.

29. Convert a JSON to a Map

This function lets us convert a Map object to a JSON string in a simple way.

jsonToMap function.
jsonToMap example

30. Generate a 128-bit UUID

This function allows us to generate a UUID with a 128-bit value for uniquely identifying an object or entity.

generateUUID function.
generateUUID example.

Final Throughs

If you have reached this point, you probably already have your own opinion about one-liners. For me, one-liners (JavaScript, Linux,…) are a compact and elegant way to solve problems, but you also have to be careful that the solution is not too difficult to read.

Other useful one-liners

https://betterprogramming.pub/25-awesome-linux-command-one-liners-9495f26f07fb

JavaScript
Javascript Tips
Javascript Development
One Liners
Web Development
Recommended from ReadMedium