How To Run JavaScript in Visual Studio Code

JavaScript is the programming language of the web. It is used on most websites and supported by most browsers, and can be run on different IDEs (integrated development environments), including Visual Studio Code. This guide shows you how to run JavaScript in Visual Studio Code in easy steps.

Tools Required To Run JavaScript In Visual Studio Code

Specific software needs to be installed on our machines to work with JavaScript.

Node.js

The first thing you’ll need is Node.js; specifically, we need the NPM package manager, which comes bundled with it. You can visit nodejs.org and download it there.

NodeJS
Download NodeJs from the official website according to your operating system.

Visual Studio Code

If you don’t already have it, the next thing you will need is Visual Studio Code itself. You can download Visual Studio Code from code.visualstudio.com.

VS Code
Download VS Code from the official website according to the version of your operating system.

How To Write JavaScript Program In Visual Studio Code?

Now that we have all the tools installed let’s create our first project. Traditionally, we will learn to print out the words “Hello World.”

Create New File

  1. Click on New File from the “File” section. You’ll see a new tab in the code editor.

    Create New File
    Click on File and then select New File.
  2. Click on Save As from the “File” section.

    Save As New File
    Click on File and then select Save As.
  3. Choose your preferred directory where you want to save the file. Then, type the file name followed by .js at the end of the file name.

    Choose the Directory
    Toggle to your preferred directory and save the file with a name followed by .js at the end of the file name.

Run JavaScript Program

Create the first basic JavaScript program that prints “Hello World” in the VS Code for testing. In the code editor, type:

 console.log("Hello World"); 
JavaScript Program Code
Write your first code in the code editor of VS Code. This code will print “Hello World.”

You’ll need a terminal window to execute the JavaScript file. Select New Terminal from the “Terminal” section to open the terminal window.

New Terminal
Click Terminal and then choose New Terminal.

This will pull up a terminal window. The terminal window allows you to run your node command to run a JavaScript file. From here, now you can run the following code to execute the file:

node index.js
Terminal Window Output
Type the given command to execute the JavaScript program. The output will be presented in the terminal window.

The syntax is simple; write node followed by the file’s name. It will run “Hello world.”

You’re all ready to use JavaScript in VS Code.

Was this article helpful?

Leave a Comment