JavaScript
Cookie Operations
Wrap Cookie read, write, and delete operations.
By EZ4Code Team
cookiebrowser
Code
const cookie = {
set(name, value, days) {
const expires = new Date(Date.now() + days * 864e5).toUTCString();
document.cookie = `${name}=${encodeURIComponent(value)}; expires=${expires}; path=/`;
},
get(name) {
const match = document.cookie.match("(^|;)\\s*" + name + "=([^;]*)");
return match ? decodeURIComponent(match[2]) : null;
},
remove(name) {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
}
};Explanation
Wraps Cookie operations, handling encoding and expiration.
More JavaScript Snippets
Array Map Filter Reduce
Chain map, filter, reduce, find, some, and every on arrays.
Array Deduplication
Deduplicate an array using Set.
Deep Clone
Deep clone objects, supporting common data types.
Debounce Function
Wait a period of time after an event triggers before executing; reset the timer if triggered again during the wait.
Throttle Function
Limit a function to execute at most once within a time interval.
Promise.all Concurrency Control
A Promise executor with concurrency limit.