Run JavaScript in Visual Studio Code

Nick Le Page picture Nick Le Page · Aug 3, 2015 · Viewed 290.7k times · Source

Is there a way to execute JavaScript and display the results using Visual Studio Code?

For example, a script file containing:

console.log('hello world');

I assume that Node.js would be needed but can't work out how to do it?

By Visual Studio Code I mean the new Code Editor from Microsoft - Not code written using Visual Studio.

Answer

Jun Han picture Jun Han · Aug 4, 2016

There is a much easier way to run JavaScript, no configuration needed:

  1. Install the Code Runner Extension
  2. Open the JavaScript code file in Text Editor, then use shortcut Control+Alt+N (or ⌃ Control+⌥ Option+N on macOS), or press F1 and then select/type Run Code, the code will run and the output will be shown in the Output Window.

Besides, you could select part of the JavaScript code and run the code snippet. The extension also works with unsaved files, so you can just create a file, change it to Javascript and write code fast (for when you just need to try something quick). Very convenient!