Sometimes we want to create html using JavaScript.
like on this page

<div > this is </div>
or may be
<b> hello </b>

for this use
var tagsToReplace = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;'

function replaceTag(tag) {
return tagsToReplace[tag] || tag;

function safe_tags_replace(str) {
return str.replace(/[&<>]/g, replaceTag);
link for fiddle is 

and resource is

There are more solutions for this

and finally my favourite one is.

function escapeHTML( string )
var pre = document.createElement('pre');
var text = document.createTextNode( string );
return pre.innerHTML;