How to Run JavaScript

By Xah Lee. Date: . Last updated: .

There are 3 major ways to run JavaScript code.

Following are details of each.

Browser JavaScript Console

A good way to learn JavaScript is to run it in browser's JavaScript console.

See: How to Use Browser Console .

Embedding JavaScript in HTML

The normal way for JavaScript to run, is to embed JavaScript code in a HTML file and view the file in browser. This is how web applications are written.

Here is how to embed JavaScript into HTML. Put the following into a file:


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

For example, name the file xtest.html. Then, open it in a browser.

Modern Way to Embed JavaScript

Modern way, since about 2010, is to put the JavaScript code in a file, and link to it, like this:

<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.

[see JS: Load Order, defer, async, module]

Using Deno

Using node.js

Alternative to node.js is deno. Ryan Dahl created node.js in 2009. Then, created a better version called deno in 2018.

I recommend Deno.

BUY Ξ£JS JavaScript in Depth