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.
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.
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
- Click on New File from the “File” section. You’ll see a new tab in the code editor.
- Click on Save As from the “File” section.
- 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.
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");
You’ll need a terminal window to execute the JavaScript file. Select New Terminal from the “Terminal” section to open the terminal window.
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
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.