I made some JavaScript snippets to save you some time. See them below and let me know your thoughts!
Contents
Detect if dark mode
This snippet detects if the user is in dark mode. And if so, it returns true.
Copy to clipboard
This snippet copies the given text to the clipboard.
Make items in an array unique
This snippet makes items in an array unique.
Get random number
Get random hex color
This snippet returns a random hex color. It first generates a random number between 0 and 255, then converts it to hex.
Shuffle array
This snippet shuffles an array.
Loop a function x times
This snippet loops a function x times. Useful if you have many loops.
Get random element from array
This snippet returns a random element from an array.
Get random string
This snippet returns a random string.
Get random boolean
This snippet returns a random boolean.
Get random number between two numbers
This snippet returns a random number between two numbers.
Get cookie
This snippet returns the value of a cookie.
Clear all cookies
Get average of numbers
Validate email
This snippet validates an email using a regex.
Validate object
This snippet validates an object using a rules object.
Chunk array
Chunks the array into x length chunks.
RGB to HEX
Raw String to normal string
This is useful if you’re having a situation like this:
Instead of doing that, you can use the power of ES6’s raw string syntax:
Compare two objects
Get selected text
Get object of query params in the url
Convert number to word
Converts large numbers like 1000000
to "1M"
.
Count number of duplicates in array
Returns an object with the number of duplicates in the array.
Generate tree from depth
If you need some testing JSON, you can use this snippet to generate a tree of depth depth
.