can we change favicon with javascript code example

Example 1: change favicon with javascript

(function() {
    var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
    link.type = 'image/x-icon';
    link.rel = 'shortcut icon';
    link.href = 'http://www.stackoverflow.com/favicon.ico';
    document.getElementsByTagName('head')[0].appendChild(link);
})();

Example 2: how to change a favicon of a website using javascript

(function() {    var link = document.querySelector("link[rel*='icon']") || document.createElement('link');    link.type = 'image/x-icon';    link.rel = 'shortcut icon';    link.href = 'https://'+window.prompt('Where is the favicon?','')+'/favicon.ico';    document.getElementsByTagName('head')[0].appendChild(link);})();