how to copy text in the clipboard in js

JavaScript
<html>
  <input type="text" value="Hello world"(Can be of your choice) id="myInput"(id is the name of the text, you can change it later)
<button onclick="Hello()">Copy Text</button>

<script>
  function Hello() {
  var copyText = document.getElementById('myInput')
  copyText.select();
  document.execCommand('copy')
  console.log('Copied Text')
}
</script>$(document).ready(function(){
	$("#ewefmwefmp").click(function(){
    //alert()
      var copyText = document.getElementById("myInput");
      copyText.select();
      copyText.setSelectionRange(0, 99999)
      document.execCommand("copy");
      alert("Copied the text: " + copyText.value);
    
    });
});<!DOCTYPE html>
<html>
 <head>
   <script>
     function paste() {
            var pasteText = document.querySelector("#text");
            pasteText.focus();
            document.execCommand("paste");

            pasteText.value = pasteText.value + pasteText.value;
        }
   </script>
  </head>
  <body>
    <input id="text">
    <button onclick="paste()">Paste</button>
   </body><script>
function copyToClipboard(element) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val($(element).text()).select();
  document.execCommand("copy");
  $temp.remove();
}
</script>

<p id="text">Hello</p>
<button onclick="copyToClipboard('#text')"></button>
Source

Also in JavaScript: