Как запустить код на javascript
Перейти к содержимому

Как запустить код на javascript

  • автор:

How to Run JavaScript Code? [Different Methods]

JavaScript is a text-based scripting language that does not need conversion to be executed. Unlike other programming languages that need compilation for their execution, JavaScript does not require pre-execution compilation.

In this article, we will learn how to run JavaScript code using different methods. There will be code snippets along with output for better understanding.

How to run JavaScript code inside Visual Studio Code

Sometimes, you may want to run your JavaScript code immediately inside Visual Studio Code (VSCode) just to see if a piece of code works. The easiest way to run JavaScript using VSCode usually involves installing Node.js locally on your machine so that you can call the script using Node.js.

For example, suppose you have a script called index.js with the following code:

To run the file, you first need to open the integrated VSCode terminal in View > Terminal menu command:

Once inside the terminal, you can then run the code using node name_of_file.js command. The picture below shows the result of running node index.js command on the terminal:

By using Node.js, you can test run any simple JavaScript code you’ve written easily from VSCode integrated terminal.

Using Code Runner Extension

Alternatively, you can also use the VSCode Code Runner Extension to run JavaScript code without having to open the console and call Node.js manually.

After you installed the extension, you just need to open the context menu on the JavaScript file you want to run with right-click, then click on the Run Code menu:

But internally, Code Runner also looks for Node.js that’s installed on your local machine, so you need to install Node.js either way.

Get 100 JavaScript Snippets Book for FREE ��

100 JavaScript snippets that you can use in various scenarios

Save 1000+ hours of research and 10x your productivity

About

Sebhastian is a site that makes learning programming easy with its step-by-step, beginner-friendly tutorials.
Learn JavaScript and other programming languages with clear examples.

Free Code Snippets Book

Get my FREE code snippets Book to 10x your productivity here

Ezoic

report this ad

How do you run JavaScript script through the Terminal?

For instance, if you were to run a Python script you would type python filename.py or if you wanted to run a C program make filename then ./ filename. How do you do this with .js files?

Ionică Bizău's user avatar

16 Answers 16

Another answer would be the NodeJS!

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Using terminal you will be able to start it using node command.

Note: If you want to exit just type

You can also run a JavaScript file like this:

loki's user avatar

Ionică Bizău's user avatar

If you have MacOS you can get jsc a javascript console by typing on Terminal.app:

On older versions of OS X, the jsc command is located at:

You could also run one of your .js script by adding its name as an argument for jsc, like this:

Notice: I use console.log() during development but jsc needs the debug() function instead.

On Ubuntu you have some nice ECMAScript shells at your disposal. Between them it’s worth to mention SpiderMonkey. You can add It by sudo apt-get install spidermonkey

On Windows as other people said you can rely on cscript and wscript directly built on the OS.

I would add also another 🙂 way of thinking to the problem, if you have time and like to learn new things i’d like to mention coffee-script that has its own compiler/console and gives you super-correct Javascript out. You can try it also on your browser (link "try coffeescript").

UPDATE July 2021: You can also install and use the brilliant QuickJS which on OS X could be installed via brew install quickjs . Then an interactive console will be available at your propmt with qjs

Как запустить код на javascript

D:/my_react_example1
index.html
my.js
.vscode
launch.json файл настройки для Visual Studio Code

Если у вас не установлена Visual Studio Code , нужно установить Visual Studio Code . .

Открываем Visual Studio Code

function calculate_sum(a, b)
<
// calculate sum
var sum = a + b;

Опять запускаем проект т.е. нажимаем RunStart Debugging и выбираем Chrome .
Появится файл с настройками launch.json .

В параметре url напишем путь к запускаемому файлу.

Запускаем программу в отладчике (debug). Нажимаем RunRun Debugging или кнопку F5 .

Программа запущена и мы можем смотреть:
WATCH (значение переменных)
CALL STACK (стэк вызова функций)
BREAKPOINTS (ставить, убирать точки остановки)

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *