DEV Community

Cover image for How to run JavaScript in Visual Studio Code
Syed Saadullah Shah
Syed Saadullah Shah

Posted on

How to run JavaScript in Visual Studio Code

VS code is a text editor made by Microsoft and it is an open-source project. It has extensions and other features for debugging your javascript, which we will talk about in this post.

Installing vs code is easy, just visit vs code's website and get the installer here: https://code.visualstudio.com/download

After you install vs code, you can create a new file (ctrl+n), name it something like "test". For this example we use test.js. You can also click on the folder icon and add the javascript there:

var s = "html > < body > <!-- // --><!-- Placeholder for VS Code generated HTML / --> < script src = "app/app.js" ></ script > </ body > </ html>
";
alert(s);
Enter fullscreen mode Exit fullscreen mode

After that's done, put "console.log('hello world')" in your javascript file! The console will pop up automatically when you click on the vs code's running tab. In the vs code window, you'll see something that says "Attach". Once you click on attach, a new vs code window will pop up and it will run your javascript file!

VS code also has extensions so you can debug your javascript better. Such as debugging multiple browsers at once or have a debugger tell vs code what to do when an exception is thrown.

To view, a list of some vs code extensions visit:

Creating a Web Server in VS Code

Now that's over with let's get into how to create a web server with vs code! You're going to want to drag and drop 2 files into vs code they are index.html and app.js. Open vs code's "settings" by right-clicking on the file name vs setting :

Click on settings, then go to "ignored files" and add ".git":

Then you want to click on "open vs editor window here", so vs code knows how to run your program:

Now you can edit your index.html file! You can also see that vs code has already added some stuff for us since it opens automatically:(but we're not going to use this yet)

VS code will try to help you out with minimal errors by telling you that vs code has attached itself to your app.js file, it also tells you that vs code's debugger is running on port 9229.

To run vs code, click on the vs code running tab and click "F5" or hit "F5". To debug HTML files, go under Tools/HTML sub vs sub ctrl+p sub debug or hit F5 :

You can edit index.html, app.js, and any other files in there! Also if you want vs code to automatically open this folder after closing it just do:

Right Click-> Open Folder Here

The end! Enjoy javascript debugging with vs code!

Discussion (0)