Html entities must be encoded in alt attribute of an image in HTML page. So
<img id="formula" alt="A → B" src="formula.png" />
will work well.
On the other hand, the same JavaScript code will not work
document.getElementById('formula').alt = 'A → B';
and will produce A → B instead of A → B.
How to do it through JavaScript, when it is not possible to put the special (unencoded) characters in the source code?
JavaScript has its own system for escaping special characters in strings:
document.getElementById('formula').alt = 'A \u2192 B';