JavaScript is one of the most important front-end languages as it grants your web page the ability to think and react. You might want to add JavaScript to your IDE if you are a front-end developer. In this tutorial, we will see how to install JavaScript in VSCode.
Install JavaScript in VSCode
Visual Studio Code is a code editor that is completely free and open-source. It has been developed by Microsoft and is highly regarded by developers due to its lightweight, fast, and extensible design. This code editor is capable of supporting various programming languages, and in this tutorial, we will be focusing on JavaScript.
To install JavaScript in VSCode, you need to follow the steps mentioned below.
- Install Visual Studio Code
- Install Node.JS
- Create and run a JavaScript program
Let us talk about them in detail.
1] Install Visual Studio Code
First of all, we need to install Visual Studio Code on your computer. To do the same, navigate to code.visualstudio.com and install the app for your preferred platform. Once downloaded, go to the Download location and install it on your computer.
2] Install Node.JS
Node.js is a free and open-source JavaScript runtime environment that can run on various operating systems such as Windows, Linux, Unix, and macOS. It uses the V8 JavaScript engine to execute JavaScript code outside of a web browser. With Node.js, you can use JavaScript to write command line tools and to perform server-side scripting.
We can install the Node.JS Extension Pack from the Extension Center. So, go to the Extension section by clicking on the respective icon, look for “Node.JS” and click on the Install icon associated with it. This package contains a collection of helpful extensions for working with JavaScript and Node.js.
To run our JavaScript code, we need to install Node.js, a JavaScript runtime. You can download it from the official Nodejs.org website. It is recommended that you choose the LTS (Long Term Support) version, as it is the most stable and suitable for most users.
3] Create and run a JavaScript program
Now that we have created an environment for the code to run, let us start coding. To do the same, you need to first create a VSCode file. So, open Visual Studio Code and go to File > New File. Give it a name of your choice but with .js extension. So, you can name it “javascript.js”. To save it use Ctrl + Shift + S or File > Save As. In order to confirm if everything is working fine, use the following command.
console.log("Hello, world!");
This JavaScript code will output “Hello, world!” via the console, a text-based interface for communication with your program.
4] Execute JavaScript file
Running JavaScript is not as straightforward as running a Java file. You either have to install the CodeRunner, which would add a button to your IDE allowing you to run the code with just a click of a button. Or, open Terminal, and then navigate to the file path using ‘cd’ or change directory command. So, just cd to your file path (cd ~/Desktop/JS) and then run node filename.js. However, Code Runner will automatically do that for you.
Read: How to configure VS Code for Microsoft C++?
Fix ‘node’ is not recognized as an internal or external command
If you get an error stating “‘node’ is not recognized as an internal or external command”, the path variable is not defined in your system. So, you need to follow the steps mentioned below to define the path in the environment variable.
- Hit Win + R, type “Environment Variable” and open the Edit the system environment variables section.
- Click on Environment Variables.
- Select Path and then click on Edit.
- Click on New, and paste the path where Nodejs is installed. For me, it was “C:\Program Files\nodejs”
- Click on Ok.
Finally, restart Visual Studio Code and then check if the issue is resolved.
Read: Visual Studio Installer stuck at installing package
How to add JavaScript in VS Code?
To add JavaScript in Visual Studio Code, you need to install VSCode first and then Node.JS. Once done, create a .JS file, write codes, and then start writing codes. To execute it, just navigate to the file path using the Terminal and then run node filename.js or better download the Code Runner extension.
Read: How to download and install Java JDK on Windows?
Can we do JavaScript in VS Code?
Yes, you can use JavaScript in VS Code. In fact, VS Code provides excellent debugging support for JavaScript. You can easily set breakpoints, inspect objects, navigate the call stack, and execute code in the Debug Console. If you want to learn more about debugging in VS Code, navigate to code.visualstudio.com.
Also Read: Best C++ Extensions for Visual Studio Code.