How to Run JavaScript in Visual Studio Code

Facebook logoTwitter logoLinkedin logo
image contains "How to Run JavaScript in Visual Studio Code" text

Visual Studio Code (VS Code) is a popular code editor that offers a plethora of features for developers. It supports various programming languages, and JavaScript is no exception.

In this blog post, we'll walk you through the steps on how to run JavaScript in Visual Studio Code, from setting up your environment to running your code.

Method 1: Using Visual Studio Code Built-in Terminal

This is a common method to run JavaScript files, you only need to have Visual Studio Code and Nodejs installed.

To install Visual Studio Code, go to this download page and choose the appropriate operating system.

Next, you need to install the latest version of Nodejs in order to run JavaScript.

How to install Nodejs

  • Go to nodejs.org
  • Download The latest version, It is Recommended For Most Users

This video will show you how to install Nodejs on your computer.

Run JavaScript in Visual Studio Code

Create a foalder and open it with Vs Code, and then Open the Built-in Terminal

Open VS Code Terminal instructions

After that create a JavaScript file and name it app.js for example, then write any console.log to view it in the terminal,

Next type this command in the terminal and click enter,

node app.js
Copy

the command node app.js will run the JavaScript file and you will see the console.log output in the terminal,

VS Code with JavaScript file open

Method 2: Using the "Code Runner" Extension

The Code Runner Extension will help you run JavaScript files it is easy to use, and It supports a lot of programming languages including JavaScript

How to install the Code Runner Extension

  • Go to the Extensions tab in VS Code
  • Search for "Code Runner"
  • Then Install it

How to use the Extension

After you install the Code Runner Extension, when you create a JavaScript file and open it in a tab in VS Code a play button will appear

When you click the play button it will run the JavaScript file, as you can see we get "Hello World" in the output as expected

javascript file open in vs code

Method 3: Debugging JavaScript Code

Visual Studio Code provides robust debugging capabilities for JavaScript.

  • Add Breakpoints: Click on the left margin of your code to add breakpoints. Breakpoints allow you to pause the execution of your code at specific lines for inspection.
  • Run in Debug Mode: To run your code in debug mode, press F5 or click the green "Run and Debug" arrow in the left sidebar.
  • Debugging Controls: You can use the debugging controls, such as stepping through the code, inspecting variables, and more.
  • View Debug Output: The debugging console at the bottom of VS Code allows you to see the output of console.log statements and error messages.

To learn more about debugging check this video

Conclusion

I trust that this guide will provide you with the knowledge to execute JavaScript in Visual Studio Code. These are the typical methods for running JavaScript in VS Code.

I hope this post has helped you to achieve your goals, Keep on coding.