How to run JavaScript in Terminal

September 29, 2020 No comments JavaScript Node Terminal

To run JavaScript code in Your Terminal first you need to install NodeJs. The next step is to open terminal and run node command (if you don’t have NodeJs then go to NodeJs Runtime Environment Download and Download it.):

1) In Windows

C:\Users\UserName> node
> console.log("Hello World"); // this is our input
Hello World
undefined
>

2) In Linux

$ node
> 2 + 4 //our input
6 //output 
> .exit // or ctrl + c

We can also use node to run the JavaScript file:

1) open terminal,

2) create an empty file: touch script.js,

3) edit file and add a simple function:

var add = (a, b) => a + b;
console.log(add(5, 10));

4) run script using node script.js command,

5) the output should be 15.

{{ message }}

{{ 'Comments are closed.' | trans }}