1️ .
Debounce Function
Delays function execution until after a specified wait time.
function debounce(func, delay) {
let timeoutId;
return function (...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => [Link](this, args), delay);
};
}
// Usage: Prevent rapid firing (e.g., search input)
const searchInput = debounce((query) => [Link](`Searching:
${query}`), 500);
searchInput("JavaScript"); // Executes after 500ms
2️.Deep Clone Object
Creates a deep copy of an object (handles nested objects & arrays).
function deepClone(obj) {
return [Link]([Link](obj));
}
// Usage: Avoid reference issues
const original = { a: 1️, b: { c: 2️ } };
const cloned = deepClone(original);
cloned.b.c = 3; // Doesn't affect original
[Link] Chunking
Splits an array into smaller chunks of a given size.
function chunkArray(arr, size) {
const chunks = [];
for (let i = 0; i < [Link]; i += size) {
[Link]([Link](i, i + size));
}
return chunks;
}
// Usage: Pagination or batch processing
[Link](chunkArray([1️, 2️, 3, 4, 5], 2️)); // [[1️, 2️], [3, 4], [5]]
[Link] Nested Arrays
Converts a nested array into a single-level array.
function flattenArray(arr) {
return [Link]((flat, item) =>
[Link]([Link](item) ? flattenArray(item) : item), []);
}
// Usage: Simplify nested data
[Link](flattenArray([1️, [2️, [3, 4], 5]])); // [1️, 2️, 3, 4, 5]
6️.Capitalize Words in a String
Capitalizes the first letter of each word in a string.
function capitalizeWords(str) {
return [Link](/\b\w/g, (char) => [Link]());
}
// Usage: Format titles/names
[Link](capitalizeWords("hello world")); // "Hello World"
7️.Check for Palindrome
Checks if a string reads the same backward as forward (case-
insensitive).
function isPalindrome(str) {
const cleaned = [Link](/[\W_]/g, "").toLowerCase();
return cleaned === [Link]("").reverse().join("");
}
// Usage: Validate palindromes
[Link](isPalindrome("A man, a plan, a canal: Panama")); // true
8️.Async Timeout with Promise
Delays execution using Promises (modern alternative to setTimeout).
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Usage: Async/await delay
async function demo() {
[Link]("Waiting...");
await delay(2️000);
[Link]("Done!");
}
demo();
9️.Random Hex Color Generator
Generates a random hexadecimal color code.
function getRandomHexColor() {
return `#${[Link]([Link]() * 0xffffff).toString(1️6️).padStart(6️,
"0")}`;
}
// Usage: Dynamic UI coloring
[Link](getRandomHexColor()); // e.g., "#3a7️bd5"