How to Run JavaScript

By Xah Lee. Date: . Last updated: .

There are few ways to run JavaScript code.

Embed JavaScript in HTML

Embed JavaScript file

<script defer src="xtest.js"></script>

you can place this anywhere inside the HTML head tag, or anywhere inside in body tag.

  1. Create a file named xtest.html, with the above content.
  2. Create a file named xtest.js, with the content alert("hi2");.
  3. Open the file xtest.html in browser.

Note: older tutorial may have type attribute like this: <script type="text/javascript" src="x.js"></script> But it is not necessary since around year 2010 with html5.

Embed JavaScript code

in HTML file, include the following

<script>
alert("hi!");
</script>

You should put this near the end of the HTML file, right above the close body tag.

Embedding code like this is not recommended, unless the code is just few lines.

Execution Order of loading JavaScript code

Browser JavaScript Console

Using Deno

Using node.js

I recommend Deno.