he (for “HTML entities”) is a robust HTML entity encoder/decoder written in JavaScript.
It supports all standardized named character references as per HTML, handles ambiguous ampersands and other edge cases just like a browser would, has an extensive test suite, and handles astral Unicode symbols just fine.