Code snippets
Code snippets
Copy-ready answers to everyday coding questions, each with a short explanation. Laravel, PHP, JavaScript, SQL, Bash and more.
2 snippets tagged #copy · Show all
JavaScript
October 14, 2026
JavaScript copy text to clipboard with the Clipboard API
Copies text with the modern async Clipboard API. It only works on HTTPS (or localhost) and usually needs to run from a user action such as a click.
async function copyText(text) {
try {
await navigator.clipboard.writeText(text);
return true;
JavaScript
October 8, 2026
Deep clone objects in JavaScript with structuredClone()
Makes a true deep copy and keeps Date, Set and Map. It avoids the JSON.parse(JSON.stringify()) hack, which turns Dates into strings and drops Sets. Needs Node 1...
const orig = { user: { name: "Ada", tags: ["a"] }, when: new Date(0), ids: new Set([1, 2]) };
const copy = structuredClone(orig);
copy.user.tags.push("b");
console.log(orig.user.tags, copy.user.tags);