How does htmlEncode convert <p> to & lt;p>?

function htmlEncode(html){
    return document.createElement("a").appendChild(document.createTextNode(html));
}
console.log(htmlEncode("

"))

output result "

"

function htmlEncode(html){
    return document.createElement("a").appendChild(document.createTextNode(html).parentNode.innerHTML);
}
console.log(htmlEncode("

"))

Why the result of the output becomes

its innerHTML is obviously

?

Jul.23,2021
Menu