 <p>You can use <code>new Function</code> for that. It wraps your code into a function like this:</p> <pre><code>var fn = new Function(output); fn(); </code></pre> <p>Or shorter:</p> <pre><code>new Function(output)(); </code></pre> <p>Which is the equivalent of:</p> <pre><code>function(){ var newElement = document.createElement("h1"); var element = document.createTextNode("Hello World!"); newElement.appendChild(element); document.body.appendChild(newElement); } </code></pre>