Skip to content

Instantly share code, notes, and snippets.

@KoljaL
Last active January 30, 2023 08:57
Show Gist options
  • Save KoljaL/fefd8f7f69591bec5d5cbe153be7a647 to your computer and use it in GitHub Desktop.
Save KoljaL/fefd8f7f69591bec5d5cbe153be7a647 to your computer and use it in GitHub Desktop.

Find the max value in an array

Math.max(...array)

Remove duplicates from an array

[...new Set(array)]

Generate a random number between 1 and 100

Math.floor(Math.random() * 100) + 1

Check if a string is a valid number

!isNaN(parseFloat(string))

Get the current date and time

new Date().toString()

Check if a variable is an array

Array.isArray(variable)

Check if a variable is an object

typeof variable === "object"

Convert an array to a string

array.join(",")

Check if a variable is a function

typeof variable === "function"

Convert an object to an array

Object.values(object)

Count the occurrences of an element in an array

array.filter(x => x === element).length

Create a new object with a dynamic key and value

{ [key]: value }

Check if a string is a palindrome

string === string.split("").reverse().join("")

Create a new array with unique values

[...new Set(array)]

Get the current timestamp

Date.now()

Check if a variable is null

variable === null

Check if a variable is undefined

typeof variable === "undefined"

Create a new array with no duplicates

[...new Set(array)]

Check if an array is empty

array.length === 0

Create a new array with a specified range of numbers

Array.from({ length: n }, (_, i) => i)

Delete a specific item from an array

arr.splice(arr.indexOf(item_to_delete),1)

Shuffle Array

const shuffleArray = (arr) => arr.sort(() => Math.random() - 0.5);

Copy to Clipboard

const copyToClipboard = (text) => navigator.clipboard?.writeText && navigator.clipboard.writeText(text);

Unique Elements

const getUnique = (arr) => [...new Set(arr)];

Detect Dark Mode

const isDarkMode = () => window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;

Scroll To Top

const scrollToTop = (element) => element.scrollIntoView({ behavior: "smooth", block: "start" });

Scroll To Bottom

const scrollToBottom = (element) => element.scrollIntoView({ behavior: "smooth", block: "end" });

Generate Random Color

const generateRandomHexColor = () => `#${Math.floor(Math.random() * 0xffffff).toString(16)}`;

Round Decimals to a Certain Number of Decimal Places

const round = (n, d) => Number(Math.round(n + "e" + d) + "e-" + d)

Capitlize A <string

const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)

Toggle Display of an Element

const toggle = element => element.style.display = (element.style.display === "none" ? "block" : "none")

Clear All Cookies

const clearCookies = document.cookie.split(';').forEach(cookie => document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date(0).toUTCString()};path=/`));

Strip HTML from Text

const stripHtml = html => (new DOMParser().parseFromString(html, 'text/html')).body.textContent || ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment