Sometimes, we want to copy output of a JavaScript variable to the clipboard.
In this article, we’ll look at how to copy output of a JavaScript variable to the clipboard.
How to copy output of a JavaScript variable to the clipboard?
To copy output of a JavaScript variable to the clipboard, we can use the navigator.clipboard.writeText
method.
For instance, we write
const copyToClipboard = (text) => {
return navigator.clipboard.writeText(text);
};
to define the copyToClipboard
function that takes the text
string parameter.
In it, we call navigator.clipboard.writeText
with text
to copy the text
string content into the clipboard.
Conclusion
To copy output of a JavaScript variable to the clipboard, we can use the navigator.clipboard.writeText
method.