How do you run JavaScript script through the Terminal?

You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName.js”. If you don’t have NodeJs runtime environment then go to NodeJs Runtime Environment Download and Download it.

Example 1: Create a JavaScript File Name this file as New.js.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple Addition Function in Javascript
function add(a, b) {
return a+b
}
console.log(add(4, 6))

chevron_right


Output:



10

Steps :

  1. Open Terminal or Command Prompt.
  2. Set Path to where File is Located (using cd).
  3. Type “node New.js” and Click Enter

Examples 2: Create a JavaScript File Name this file as New2.js.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple Substraction Function in Javascript
function sub(a, b) {
return a-b
}
console.log(sub(6, 2))

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Adorably Eccentric

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.