In this short tutorial, you will learn how to run JavaScript in Visual Studio Code. There are three ways you can run JavaScript in the text editor and for that, feel free to choose whatever suits you the best.

Method 1: Use HTML script tag

The simplest way to run JavaScript in VS Code is to create an HTML file. This method is straightforward as you only need VS Code and a browser of your choice to view JavaScript outputs. Here’s how you will do it:

  1. Create an HTML file in VS Code and inside the file, add the script tags. You can write JavaScript code inside the script tags.
  2. Save the changes and then open the HTML file in the browser.
  3. In the browser, open the inspect window by right-clicking the mouse and select Inspect option. You can also open it by clicking Ctrl + Shift + I keys for Windows, or Cmd + Option + I keys for Mac.
  4. In the Inspect Window, click the Console tab, and this is where you can see the JavaScript outputs.

The only downside of using this approach is you will constantly switch between a browser and VS Code windows. Unless you’re working on smaller windows or have two monitor screens, this will inconvenience you a little bit.

Method 2: Use Node.js

Another way to run JavaScript in VS Code is using Node.js. This method will let you run JavaScript files directly on the text editor’s terminal, and no need to switch windows like in method 1. Here are the steps to run JavaScript with Node.js:

  1. Download and install Node.js on your computer.
  2. Open the JavaScript file that you want to run it.
  3. Open VS Code’s terminal by clicking Terminal > New Terminal, or clicking Ctrl + Shift + ` keys (Cmd + Shift+ P for Mac).
  4. In the terminal, type node file_name.js where file_name.js is the name of your JavaScript file. For example, if your file name is app.js, write node app.js click enter key to see the JavaScript output.

Method 3: Use Code Runner Extension

If you don’t like to run JavaScript manually, you can use the Code Runner Extension. While you don’t need to configure anything, you still have to install Node.js on your computer. To run JavaScript on Code Runner Extension:

  1. Click the Extensions tab, and search for Code Runner and install it.
  2. Open any JavaScript file and to run it, right-click the mouse and click the Run Code option or Ctrl + Alt + N. It will open the text editor’s Output window, and you can see the JavaScript outputs.

Get my free e-book to prepare for the technical interview or start to Learn Full-Stack JavaScript