My top JavaScript utilities, in just One Line of Code!
What's your favorite one-liner in JavaScript?
Here are mine. And if you're a TypeScript fan, you'll be happy to know that most of these snippets work in TypeScript too.
Array
Intersperse element between elements
24 Dec, 2021
Generate an array of alphabet characters
22 Oct, 2021
Repeat an array
21 Jul, 2021
Find the index of the minimum item of an array
20 Apr, 2021
Find the index of the maximum item of an array
20 Apr, 2021
Get all arrays of consecutive elements
10 Apr, 2021
Cast a value as an array
04 Apr, 2021
Swap two array items
13 Mar, 2021
Remove duplicate values in an array
07 Mar, 2021
Get indices of a value in an array
07 Mar, 2021
Get all subsets of an array
25 Feb, 2021
Get all n-th items of an array
20 Feb, 2021
Sort an array of items by given key
13 Jan, 2021
Find the minimum item of an array by given key
13 Jan, 2021
Find the maximum item of an array by given key
13 Jan, 2021
Create cartesian product
02 Dec, 2020
Check if an array is empty
23 Jun, 2020
Count by the properties of an array of objects
14 Jun, 2020
Count the occurrences of array elements
11 Jun, 2020
Count the occurrences of a value in an array
11 Jun, 2020
Group an array of objects by a key
03 Jun, 2020
Swap the rows and columns of a matrix
24 May, 2020
Find the closest number from an array
22 May, 2020
Sort an array of numbers
20 May, 2020
Shuffle an array
20 May, 2020
Unzip an array of arrays
15 May, 2020
Zip multiple arrays
13 May, 2020
Split an array into chunks
13 May, 2020
Partition an array based on a condition
13 May, 2020
Compare two arrays regardless of order
11 May, 2020
Remove falsy values from array
09 May, 2020
Get union of arrays
09 May, 2020
Get the intersection of arrays
09 May, 2020
Clone an array
07 May, 2020
Compare two arrays
05 May, 2020
Convert an array of objects to a single object
04 May, 2020
Flatten an array
01 May, 2020
Convert an array of strings to numbers
23 Apr, 2020
Merge two arrays
19 Apr, 2020
Get the unique values of an array
19 Apr, 2020
Get the sum of an array of numbers
19 Apr, 2020
Get the rank of an array of numbers
19 Apr, 2020
Get the average of an array
19 Apr, 2020
Find the minimum item of an array
19 Apr, 2020
Find the maximum item of an array
19 Apr, 2020
Empty an array
19 Apr, 2020
Create an array of numbers in the given range
19 Apr, 2020
Create an array of cumulative sum
19 Apr, 2020
DOM
Calculate the distance to the bottom of a page
23 Aug, 2023
Check if user scrolls to the bottom of the page
10 Apr, 2021
Serialize form data
01 Feb, 2021
Detect Internet Explorer browser
09 May, 2020
Check if the touch events are supported
25 Apr, 2020
Toggle an element
19 Apr, 2020
Strip HTML from a given text
19 Apr, 2020
Show an element
19 Apr, 2020
Scroll to top of the page
19 Apr, 2020
Replace an element
19 Apr, 2020
Reload the current page
19 Apr, 2020
Redirect to another page
19 Apr, 2020
Insert given HTML before an element
19 Apr, 2020
Insert given HTML after an element
19 Apr, 2020
Insert an element before other one
19 Apr, 2020
Insert an element after other one
19 Apr, 2020
Hide an element
19 Apr, 2020
Go back to the previous page
19 Apr, 2020
Get the selected text
19 Apr, 2020
Get all siblings of an element
19 Apr, 2020
Detect macOS browser
19 Apr, 2020
Check if an element is focused
19 Apr, 2020
Check if an element is a descendant of another
19 Apr, 2020
Date Time
Get the hours and minutes from the decimal time
27 Dec, 2022
Determine one year from now
21 Aug, 2022
Get the total number of days in a year
10 Apr, 2021
Get the timezone string
25 Feb, 2021
Get the last date in the month of a date
25 Feb, 2021
Get the first date in the month of a date
25 Feb, 2021
Get the current quarter of a date
25 Feb, 2021
Add AM PM suffix to an hour
20 Feb, 2021
Calculate the number of months between two dates
27 Jul, 2020
Get the yesterday date
21 May, 2020
Get the tomorrow date
21 May, 2020
Get the day of the year from a date
16 May, 2020
Convert seconds to hh:mm:ss format
15 May, 2020
Format a date for the given locale
06 May, 2020
Get the weekday of a date
05 May, 2020
Get the month name of a date
05 May, 2020
Sort an array of dates
19 Apr, 2020
Get the number of days in given month
19 Apr, 2020
Get the current timestamp in seconds
19 Apr, 2020
Convert a date to YYYY-MM-DD format
19 Apr, 2020
Compare two dates
19 Apr, 2020
Function
Create a function that accepts a single argument
13 Mar, 2021
Box handler
15 Jul, 2020
Memoize a function
16 May, 2020
Uncurry a function
15 May, 2020
Curry a function
15 May, 2020
Check if a value is an async function
15 May, 2020
Check if a value is a generator function
15 May, 2020
Check if a value is a function
15 May, 2020
Partially apply a function
13 May, 2020
Logical xor operator
13 May, 2020
Execute a function once
13 May, 2020
Flip the arguments of a function
12 May, 2020
Delay the evaluation of a function
12 May, 2020
Compose functions from left to right
12 May, 2020
Identity function
07 May, 2020
Create an empty function
07 May, 2020
Compose functions
07 May, 2020
Math
Linear scale of a number between two ranges
28 Sep, 2023
Normalize the ratio of a number in a range
24 May, 2021
Calculate the midpoint between two points
10 Apr, 2021
Check if a rectangle overlaps other one
08 Apr, 2021
Check if a rectangle contains other one
08 Apr, 2021
Convert radians to degrees
05 Apr, 2021
Check if a point is inside a rectangle
05 Apr, 2021
Calculate the distance between two points
05 Apr, 2021
Convert degrees to radians
16 May, 2020
Misc
Convert an LCH color to a LAB color
10 Nov, 2023
Convert a LAB color to an LCH color
10 Nov, 2023
Check if the code is running in Jest
18 Jun, 2022
Clear all cookies
20 Apr, 2021
Convert Fahrenheit to Celsius
08 Apr, 2021
Generate an unique and increment id
05 Apr, 2021
Convert 3 digits color to 6 digits color
20 Feb, 2021
Convert URL parameters to object
01 Feb, 2021
Get type of a variable in string
12 Jan, 2021
Convert cookie to object
27 Jun, 2020
Get the first defined and non null argument
12 Jun, 2020
Redirect the page to HTTPS if it is in HTTP
31 May, 2020
Get the value of a cookie
28 May, 2020
Decode a JWT token
23 May, 2020
Convert Celsius to Fahrenheit
09 May, 2020
Wait for an amount of time
06 May, 2020
Encode a URL
03 May, 2020
Run Promises in sequence
02 May, 2020
Detect dark mode
02 May, 2020
Check if the code is running in the browser
02 May, 2020
Check if the code is running in NodeJS
02 May, 2020
Emulate a dice throw
01 May, 2020
Easing functions
25 Apr, 2020
Get the value of a param from a URL
23 Apr, 2020
Swap two variables
19 Apr, 2020
Convert rgb color to hex
19 Apr, 2020
Convert hex to rgb
19 Apr, 2020
Number
Wrap a number between two values
13 Dec, 2021
Convert decimal to binary recursively
11 Dec, 2020
Truncate a number at decimal
05 Aug, 2020
Convert a number to equivalent characters
27 May, 2020
Calculate the factorial of a number
19 May, 2020
Round a number to a given number of digits
16 May, 2020
Get the arrays of digits from a number
16 May, 2020
Calculate Fibonacci numbers
16 May, 2020
Add an ordinal suffix to a number
16 May, 2020
Calculate the mod of collection index
08 May, 2020
Subtract arguments
05 May, 2020
Multiply arguments
05 May, 2020
Clamp a number between two values
05 May, 2020
Calculate the sum of arguments
05 May, 2020
Calculate the remainder of division of arguments
05 May, 2020
Calculate the division of arguments
05 May, 2020
Calculate the average of arguments
05 May, 2020
Prefix an integer with zeros
23 Apr, 2020
Convert a string to number
19 Apr, 2020
Object
Returns an object with unique values by key
17 Feb, 2023
Sort an object by its properties
20 Apr, 2021
Immutably rename object keys
11 Jan, 2021
Check if multiple objects are equal
20 May, 2020
Create an object from the pairs of key and value
18 May, 2020
Invert keys and values of an object
15 May, 2020
Shallow copy an object
13 May, 2020
Pick a subset of properties of an object
13 May, 2020
Omit a subset of properties from an object
13 May, 2020
Get the value at given path of an object
13 May, 2020
Random
Generate a weighted random number in given range
22 Aug, 2023
Pick random lines from a text document
01 Jun, 2021
Pick a random property of an object
19 Apr, 2021
Generate a random sign
10 Apr, 2021
Get random items of an array
16 Jan, 2021
Get a random item and remove it from an array
16 Jan, 2021
Generate a random IP address
10 Aug, 2020
Generate a random UUID
05 May, 2020
Generate a random integer in given range
05 May, 2020
Generate a random boolean
05 May, 2020
Generate a random string with given length
23 Apr, 2020
Generate a random string from given characters
23 Apr, 2020
Get a random item from an array
19 Apr, 2020
Generate a random hex color
19 Apr, 2020
String
Capitalize a string
11 Apr, 2023
Decode HTML entities
21 Aug, 2022
Format a string
10 Nov, 2021
Remove duplicate lines of a text document
31 May, 2021
Reverse the order of lines of a text
30 May, 2021
Remove empty lines of a text document
28 May, 2021
Replace all tab characters with spaces
27 May, 2021
Count the number of words in a string
26 May, 2021
Count the occurrences of a character in a string
19 Apr, 2021
Generate a hash of a string
21 Mar, 2021
Decapitalize a string
21 Feb, 2021
Swap case of characters in a string
20 Feb, 2021
Get the number of a character in a string
02 Aug, 2020
Convert a letter to associate emoji
02 Aug, 2020
Get the base URL without any parameters
20 Jun, 2020
Strip ANSI codes from a string
07 Jun, 2020
Trim some character
04 Jun, 2020
Trim the file extension from a file name
30 May, 2020
Unescape HTML special characters
29 May, 2020
Truncate a string at full words
29 May, 2020
Escape HTML special characters
29 May, 2020
Convert the name of an Excel column to number
25 May, 2020
Check if a string is a palindrome
18 May, 2020
Get the length of a string in bytes
16 May, 2020
Convert a string to camelCase
16 May, 2020
Normalize file path slashes
15 May, 2020
Convert a Windows file path to Unix path
15 May, 2020
Convert a string to PascalCase
15 May, 2020
Check if a URL is absolute
15 May, 2020
Check if a path is relative
15 May, 2020
Convert snake_case to camelCase
12 May, 2020
Convert camelCase to kebab-case and vice versa
12 May, 2020
Replace multiple spaces with a single space
09 May, 2020
Check if two strings are anagram
07 May, 2020
Convert a string to URL slug
04 May, 2020
Remove spaces from a string
03 May, 2020
Make the first character of a string lowercase
03 May, 2020
Reverse a string
19 Apr, 2020
Replace all line breaks with br elements
19 Apr, 2020
Repeat a string
19 Apr, 2020
Get the file name from a URL
19 Apr, 2020
Get the file extension from a file name
19 Apr, 2020
Validator
Check if a character is a digit
22 Oct, 2021
Check if a date occurs in the current year
13 Apr, 2021
Check if a value is base64 encoded
10 Apr, 2021
Check if a value is base58 encoded
10 Apr, 2021
Check if a value is base32 encoded
10 Apr, 2021
Check if a value is a business identifier code
10 Apr, 2021
Check if a string is an octal number
10 Apr, 2021
Check if a string is a MongoDB ObjectId
10 Apr, 2021
Check if a flat array has duplicate values
25 Feb, 2021
Check if a date is a weekend
25 Feb, 2021
Check if a date is a weekday
25 Feb, 2021
Check if a string is a hexadecimal number
10 Jun, 2020
Check if a string is a hexadecimal color
09 Jun, 2020
Check if a string contains only ASCII characters
09 Jun, 2020
Check if a string contains only letters
07 Jun, 2020
Check if a value is a string
05 Jun, 2020
Check if a number is in a given range
04 Jun, 2020
Check if a string contains upper case characters
16 May, 2020
Check if a string contains lower case characters
16 May, 2020
Check if a value is a regular expression
14 May, 2020
Check if a value is an object
13 May, 2020
Check if a value is a plain object
13 May, 2020
Check if an array is subset of other array
10 May, 2020
Check if a string contains only digits
09 May, 2020
Check if a number is positive
07 May, 2020
Check if a number is negative
07 May, 2020
Check if an object is a Promise
06 May, 2020
Check if an array is not empty
06 May, 2020
Check if a value is nil
06 May, 2020
Check if a number is odd
06 May, 2020
Check if a number is even
06 May, 2020
Check if a date is today
05 May, 2020
Validate a Gregorian date
03 May, 2020
Check if a value is a number
23 Apr, 2020
Check if an object is empty
19 Apr, 2020
Check if an object is an array
19 Apr, 2020
Check if all items in an array are equal
19 Apr, 2020
Check if a year is leap year
19 Apr, 2020
Check if a string is upper case
19 Apr, 2020
Check if a string is lower case
19 Apr, 2020
Check if a string contains whitespace
19 Apr, 2020
Check if a number is a power of 2
19 Apr, 2020
Check if a given integer is a prime number
19 Apr, 2020
Check if a date is between two dates
19 Apr, 2020
Recent posts ⚡
Use forwardRef with a higher-order component
05 Dec, 2023
Swap items in a sortable list
04 Dec, 2023
Display a placeholder in a sortable list
03 Dec, 2023
Create a sortable list
02 Dec, 2023
Build a drop zone for file uploads
01 Dec, 2023
Read the content of the dropped file
30 Nov, 2023
Preview images before uploading them
29 Nov, 2023
Build a drop file indicator
28 Nov, 2023
Create your own ghost element
27 Nov, 2023
Customize the appearance of a ghost element
26 Nov, 2023
An introduction to HTML 5 drag and drop
25 Nov, 2023
Use a custom drag handle
24 Nov, 2023
Create a linear gauge with discrete values
22 Nov, 2023
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn