Categories
JavaScript Answers

How to copy output of a JavaScript variable to the clipboard?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *