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 VS 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 a JavaScript program in VS 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.”

Step 1: Create a 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 File, 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.

Step 2: 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, you can now 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.

Leave a Comment