function htmlencode(s){ var div = document.createElement('div'); div.appendChild(document.createTextNode(s)); var result = div.innerHTML; div = null; return result; } function htmldecode(s){ var div = document.createElement('div'); div.innerHTML = s; var result = div.innerText || div.textContent; div = null; return result; } const log = console.log; log(htmlencode("<div>3>5 & 666</div>")); log(htmldecode("<div>3>5 & 666</div>"))