Each developer has their own "backpack" with utilities that help solve everyday tasks faster. This article contains ten concise but extremely useful JS snippets that can be added to each project. They are independent of frameworks and work great in any environment: from Node.js to the browser.
1. 📅 Date formatting
const formatDate = (date) =>
new Intl.DateTimeFormat('ru-RU', {
dateStyle: 'medium',
timeStyle: 'short',
}).format(date);❓Why: display the date and time beautifully and localized.
Example: formatDate(new Date()) // "June 29, 2025, 12:00"
2. 🧪 Check: is it a prank or not
const isPromise = (val) =>
Boolean(val && typeof val.then === 'function');❓Why: we determine that we are dealing with a promiscuous person.
3. 📜 Copying to clipboard
const copyToClipboard = async (text) =>
await navigator.clipboard.writeText(text);❓Why: often you need to implement "Copy" on click.
4. 🔁 Deep object copying
const deepClone = (obj) =>
JSON.parse(JSON.stringify(obj));❓Why: so as not to mutate the original object.
5. ⏳ Simple debounce
const debounce = (fn, delay = 300) => {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
};❓Why: to optimize frequent events, such as input.
6. 🚫 Removing duplicates from an array
const unique = (arr) => [...new Set(arr)];❓Why: we clean the array from repetitions elegantly.
7. 🆔 Generating a simple ID
const randomId = () =>
Math.random().toString(36).slice(2, 10);❓Why: fast unique value for UI or keys.
8. ⏱ Async/await style delay
const sleep = (ms) =>
new Promise((resolve) => setTimeout(resolve, ms));❓Why: simulation of delay, pauses in animation, tests, etc.
9. 💬 Getting parameters from the URL
const getQueryParam = (key) =>
new URLSearchParams(window.location.search).get(key);❓ Why: often needed - when filtering, pagination, etc.
10. 🧼 Cleaning the object from empty values
const cleanObject = (obj) =>
Object.fromEntries(
Object.entries(obj).filter(([_, v]) => v != null)
);❓ Why: remove null and undefined before sending data.
🤔 Why memorize these snippets?
📌 They decide typical tasks, found in any project.
📌 Accelerate development and make the code cleaner.
📌 Understandable to beginners, useful to experienced.
If you are learning to program and want to level up daily, check out our app Code. This is an exciting way to learn programming through practice and challenges!
Web version of Kodik - https://itcodik.com/
📦 Tip
Create a file utils.js and store your collection of favorite utilities there — like a personal JS Swiss knife 🛠
