Not all JavaScript programs are the same as you can use JS for backend, frontend and mobile development. Running JavaScript code depends on what kind of code it is.

Frontend JS

If you’re looking to run the JavaScript code that manipulates the DOM (document object model) and works within the context of the web page, then the best way to “run it”, would be to link the JS script to the HTML document and open this HTML in the browser.

Let’s start with the file index.js.

alert('Hello, world!');

Now, let’s proceed by creating a file index.html and linking the index.js file to it in the <head> section.

  <script src="index.js"/>

When you open the file index.html in the browser you’ll see the alert with the next Hello, world! popping right in your face.

Backend JS

To run backend JavaScript code you’ll need to install Node.js on your computer. Once it’s done, you need a JS file.

Let it be index.js once again.

console.log('Hello, world!');

This time, to run the JS code you need to open your terminal and type the node command to let Node.js run your JavaScript code.

node index.js


Hello, world!

Notice that when you work with the backend JavaScript, you can’t use things like alert as they only work in the browser.