---
title: "Discussion on \"How to copy to clipboard in JavaScript?\" | Hashnode"
description: "Discussion on \"How to copy to clipboard in JavaScript?\". JavaScript Web APIs got a bunch of methods to enhance the user experience of web applications. Learn how Copy-Paste is possible using JavaScript."
url: "https://hashnode.com/posts/how-to-copy-to-clipboard-in-javascript/626f7b5da2d355d4df441a5f"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How to copy to clipboard in JavaScript?](https://blog.greenroots.info/how-to-copy-to-clipboard-in-javascript?utm_source=hashnode&utm_medium=feed)

JavaScript Web APIs got a bunch of methods to enhance the user experience of web applications. Learn how Copy-Paste is possible using JavaScript.

May 2, 2022 · 2 min read · blog.greenroots.info  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#programming](https://hashnode.com/tag/programming)
-   [#web-development](https://hashnode.com/tag/web-development)
-   [#beginners](https://hashnode.com/tag/beginners)
-   [#codenewbies](https://hashnode.com/tag/codenewbies)

## Written by

[Tapas Adhikary](https://hashnode.com/@atapas)

@atapas · Bangalore

  
[View profile](https://hashnode.com/@atapas)

Educator @tapaScript | Founder CreoWis & ReactPlay - Writer - YouTuber - Open Source

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human

## Responses 3

Markdown supported · ⌘↵ to post

[boystrong1001 eddy](https://hashnode.com/@jamescott)

[Sep 12, 2022](https://hashnode.com/posts/how-to-copy-to-clipboard-in-javascript/626f7b5da2d355d4df441a5f/comment/631f5159526d775aa7f6598d)

Very good article, thanks

[Tapas Adhikary](https://hashnode.com/@atapas)

[Sep 13, 2022](https://hashnode.com/posts/how-to-copy-to-clipboard-in-javascript/626f7b5da2d355d4df441a5f/comment/631f5159526d775aa7f6598d#reply-63202208526d775aa7f67a8e)

Thank you [boystrong1001 eddy](https://hashnode.com/@jamescott)

[Hrithwik Bharadwaj](https://hashnode.com/@hrithwik)

[May 4, 2022](https://hashnode.com/posts/how-to-copy-to-clipboard-in-javascript/626f7b5da2d355d4df441a5f/comment/6271e052d64dcf11c4fb61da)

Even I use Clipboard API but with the support for fallback like below,

(FB/Instagram browser doesn't support just clipboard API)

```
 async function copyLink(){
      try{
         await navigator.clipboard.writeText(link);
         console.log("Link Copied");
     }
      catch(e){
         copyToClipbordFallback(link);
         console.log("Link Copied");
      }
    }

function copyToClipbordFallback(text){
      const textArea = document.createElement("textarea");
      textArea.value = text;
      document.body.appendChild(textArea);
      textArea.select();
      document.execCommand("copy");
      textArea.remove();
}
```

so this fallback is mainly for FB/IG Browsers and other old browsers

[Tapas Adhikary](https://hashnode.com/@atapas)

[May 4, 2022](https://hashnode.com/posts/how-to-copy-to-clipboard-in-javascript/626f7b5da2d355d4df441a5f/comment/6271e052d64dcf11c4fb61da#reply-6271ec8bf602e011e9f675b6)

Yes that's a good callout [Hrithwik](https://hashnode.com/@hrithwik)

[Diego Ballesteros (Relatable Code)](https://hashnode.com/@diegoballesteros)

[May 2, 2022](https://hashnode.com/posts/how-to-copy-to-clipboard-in-javascript/626f7b5da2d355d4df441a5f/comment/626fcf9ffa2b07f7d96a84a2)

Glad it's being suggested to use the Clipboard API and not the now deprecated one that you sometimes see floating around on "JavaScript Tips" on Twitter.

For reference:

```
execCommand('copy')
```

_don't use that_

[Tapas Adhikary](https://hashnode.com/@atapas)

[May 2, 2022](https://hashnode.com/posts/how-to-copy-to-clipboard-in-javascript/626f7b5da2d355d4df441a5f/comment/626fcf9ffa2b07f7d96a84a2#reply-626fd46ffa2b07f7d96a8566)

Absoloutely [Diego](https://hashnode.com/@diegoballesteros)
