Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to run JavaScript code in visual studio code

Setting Up Your Environment

Before we start discussing how to run JavaScript (JS) code in Visual Studio Code (VSCode), let's first set up your environment. Visual Studio Code is a 'code editor'. Think of it as a word processor, but instead of writing essays, you write code in it.

  1. Download and install VSCode if you haven't already.
  2. Open VSCode. Click on the 'Extensions' button on the Activity Bar (the bar on the side of the window). In the search bar, type 'Code Runner'. Install the extension that comes up.

Congratulations! You've set up your environment. Now, let's learn how to use it.

Creating a JavaScript File

  1. Click on 'File' > 'New File' or press Ctrl+N to create a new file.
  2. Click on 'File' > 'Save As', or press Ctrl+S to save the file. Give it a name that ends with .js, for example, myFirstProgram.js. The .js extension lets VSCode know that this is a JavaScript file.

Writing JavaScript Code

Let's write a simple JS program. In your new JS file, type the following:

console.log('Hello, World!');

The console.log() function is a way to output data. In this case, it will output the string 'Hello, World!'.

Running Your JavaScript Code

Here comes the exciting part - running your code. Right-click anywhere in your code file and select 'Run Code'.

You will see the output in the 'Output' panel at the bottom. It should show 'Hello, World!'.

Debugging Your JavaScript Code

Sometimes, things don't go as planned. Maybe there's a typo in your code, or you've forgotten to add something. This is where debugging comes in. Debugging is like being a detective, solving the mystery of why your code isn't working.

  1. Click on the 'Debug' button on the Activity Bar.
  2. Click on 'Run and Debug'.
  3. Choose 'Node.js' (This is the JavaScript runtime environment that allows you to run JS on your machine).

Your code will run, and if there's an error, it will stop at the line where the error occurred, allowing you to inspect it and figure out what went wrong.

Using JavaScript Libraries

Libraries are collections of pre-written code that you can use to speed up your coding. Think of them as a toolbox, filled with tools (functions and methods) that you can use.

To use a library, you have to first import it. Let's use the popular library 'lodash' as an example.

  1. First, we need to install the library. Open the terminal in VSCode (Terminal > New Terminal), and type npm install lodash.
  2. At the top of your JS file, type const _ = require('lodash');. This loads the lodash library and assigns it to the variable _.
  3. Now you can use lodash functions. For example, type console.log(_.last([1, 2, 3]));.

When you run your code now, it should output '3', which is the last element of the array.


Running JavaScript in Visual Studio Code is as simple as penning down your thoughts in a diary. With just a few steps - setting up the environment, writing and saving your code, you are ready to see your thoughts come to life in the output panel. And just like a diary helps you make sense of your thoughts, debugging in VSCode helps you understand the logic of your code better.

Remember, every single coder started where you are right now. Each line of code you write is a step towards becoming a proficient programmer. So, keep practicing, keep coding, and you'll soon be creating programs that solve real-world problems. Happy coding!