boost organic

How to run JavaScript in Visual Studio Code

Visual Studio Code (VSCode) has several built-in features like debugging, formatting, code navigation, and jаvascript IntelliSense. If someone wants to check whether a piece of code works or not then you might want to run jаvascript in VSCode.

This article conveys the execution of jаvascript in VSCode. The steps to run jаvascript inside Visual Studio are as follows:


1. The first step is the installation of Node.js on your MacBook/Windows in order to call scripts through Node.js. You can easily download Node.js by visiting


2. In the second step you have to create a new folder then open this folder in Visual Studio Code. Subsequently, write jаvascript code and save it with an extension of “.js”.


3. Open up the operating system’s terminal inside Visual Studio Code by clicking on View on the topmost bar.



Example


To run a script named index.js in Visual Studio Code then you should first make sure that node.js is installed. Open the terminal within Visual Studio Code. You can now easily run jаvascript in the terminal of VSCode by using node.js. The syntax of the node command used to run jаvascript code is shown in the VSCode terminal.


$ node [filename].js

In this example, we wrote the script to check if the number is even or odd. Subsequently, we execute the command in the terminal as shown below to get the output. In this case, we assigned 6 to the num variable and it gives the following output.


// this script used to check if the number is even or odd



const num = 6;



const result = (num % 2  != 0) ? "odd" : "even";



// display the result



console.log(`Number is ${result}.`);

Output



On the other hand, we assigned 43 to the num variable and it gives the following output.


// this script used to check if the number is even or odd



const num = 43;



const result = (num % 2  != 0) ? "odd" : "even";



// display the result



console.log(`Number is ${result}.`);

Output



An alternative way to run jаvascript in VSCode using Code Runner Extension


This is the simplest method to run jаvascript. There are no configurations required in this method. You need to install Node.js either way on your machines as the code runner extension also needs Node.js. Following steps must be kept in mind to run jаvascript in VSCode using a code runner extension.


1. First of all, you need to install Code Runner Extension in order to run jаvascript code. This extension will consume a couple of minutes to install and it is really easy to install it. You can easily download the code runner extension by visiting



Then click on the Open URL:vscode button. The following window is shown as mentioned below.



2. After installation of the code runner extension, open jаvascript Code in VSCode. Press CTRL+ALT+N shortcut or you may press F1 then write Run Code to run the code. Subsequently, you will see the following output in the “OUTPUT” tab.


// this script used to check if the number is even or odd



const num = 6;



const result = (num % 2  != 0) ? "odd" : "even";



// display the result



console.log(`Number is ${result}.`);

Output:



Conclusion


Visual Studio gives quality, flexibility and also gives outstanding debugging experience to web applications using jаvascript. So, in order to run/execute jаvascript in Visual Studio Code we need NodeJS which acts as an interpreter. In this article, we demonstrate how to run jаvascript in Visual Studio Code and also explain an alternative way to run jаvascript in VSCode using Code Runner Extension along with detailed examples.

[fixed][/fixed]