How to Run JavaScript in Visual Studio Code

Mehvish Ashiq Feb 02, 2024
  1. Run JavaScript Code in Visual Studio Code Terminal
  2. Run JavaScript Code in Visual Studio Code Using Code Runner Extension
How to Run JavaScript in Visual Studio Code

This article aims to provide you with step-by-step guidance about running JavaScript code in Visual Studio Code. It will cover two methods.

  • Run on Visual Studio Code Terminal
  • Run on Visual Studio Code via Code Runner Extension

Run JavaScript Code in Visual Studio Code Terminal

It is assumed that you have created a test.js file in a folder named JS Code that resides on Desktop. It is good to create one to follow along with this tutorial.

Open that folder in Visual Studio Code by going to File>Open Folder.

how to run javascript code in visual studio code - open folder in vscode

The following pop-up will appear, select your desired folder and click on the Select Folder button. See the screenshot below.

how to run javascript code in visual studio code - select folder to open

We have our test.js file opened in Visual Studio Code. Have a look at the screenshot below.

If you don’t see the Terminal window (see number 1), then go to Terminal>New Terminal or press Ctrl+Shift+` to open a new one (see number 2).

how to run javascript code in visual studio code - terminal

Let’s run the code by typing node filename.js; it would be node test.js in this case. Remember, you must be on the path where your .js file resides.

We are getting the error saying The term 'node' is not recognized (see the following) because we don’t have Node.js installed.

If you have it already, you must have the output, otherwise, download it from here and follow these guidelines to install.

how to run javascript code in visual studio code - terminal error

Are you done with Node.js installation? Let’s type node test.js again and see the output below.

how to run javascript code in visual studio code - terminal output

Run JavaScript Code in Visual Studio Code Using Code Runner Extension

We install Code Runner Extension in Visual Studio Code to repeatedly remove typing commands. It handles everything for you.

You only have to open the JavaScript file in Visual Studio Code and press Ctrl+Alt+N to get the output.

We are getting the answer under the output tab. See the output below.

how to run javascript code in visual studio code - code runner output

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook