js document,appendchild code example

Example 1: appendchild javascript

function createMenuItem(name) {
    let li = document.createElement('li');
    li.textContent = name;
    return li;
}
// get the ul#menu
const menu = document.querySelector('#menu');
// add menu item
menu.appendChild(createMenuItem('Home'));
menu.appendChild(createMenuItem('Services'));
menu.appendChild(createMenuItem('About Us'));

Example 2: appendchild element once if element present in js

<script>
       var button = false;
        function  myfun(){
            if ( button === false){
                var b1 = document.createElement("BUTTON");
                b1.innerHTML = "Click Me";
                document.body.appendChild(b1); 
                return button = true;  
            }
      }
    </script>
<p id="demo" onclick="myfun();"> click me to create button </p>