Open In App

How to Run JavaScript in Visual Studio?

Last Updated : 27 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To run JavaScript in Visual Studio, you can either use Node.js in the Terminal or the Code Runner extension. Both methods allow you to execute JavaScript code easily and efficiently within the Visual Studio environment.

Using Node.js in Terminal

Node.js is a JavaScript runtime that allows you to execute JavaScript outside of a web browser. You can run JavaScript files directly from the terminal in Visual Studio.

Steps to Run JavaScript Using Node.js

Step 1: Install Node.js

  • Download and install Node.js by following our installation guide
  • Verify the installation by running the following command in the terminal
node -v

Step 2: Create a New JavaScript File

  • In Visual Studio, create a new file with a .js extension.
  • Example: script.js

Step 3: Write Your JavaScript Code

Add your JavaScript code to the newly created file.

JavaScript
function hello() {
    console.log("Hllo Geek, Welcome to GFG!");
}
hello();

Step 4: Open Terminal

  • Open the terminal in Visual Studio by navigating to View > Terminal or pressing Ctrl + (backtick).
  • Navigate to the folder containing your JavaScript file using the cd command.
cd <foldername>

Step 5: Run Your JavaScript File

Execute the file using the following command:

node <filename>.js
js1
Running JavaScript code using Node.js

Using code runner extension

The Code Runner extension provides a quick way to run JavaScript directly in Visual Studio without using the terminal.

Step 1: Open Visual Studio

Launch Visual Studio.

Step 2: Access Extensions View

Click on the Extensions icon in the sidebar or press Ctrl + Shift + X.

Step 3: Install Code Runner Extension

  • Search for "Code Runner" in the Extensions view.
  • Click on Install to add the extension to Visual Studio.

Step 4: Open JavaScript File

Open the JavaScript file you want to run.

Step 5: Execute the Code

  • Right-click on the JavaScript code and select Run Code.
  • Alternatively, press Ctrl + Alt + N to run the code.
Screenshot-(79)
Running JavaScript code using the runner extension.

Next Article

Similar Reads