How do you run JavaScript file through the Terminal?

There might be a situation when you need to check your JavaScript code without using a browser, in that situation what should we do? First of all you might be surprised “JavaScript code without browser?”. Yes, it is possible by using NodeJs Runtime Environment. You can run your JavaScript file through the terminal only if you have installed NodeJs runtime.

If you don’t have NodeJs runtime environment in your system then go to NodeJs Environment Download and download it. Next, you have to create a JavaScript file and name it as Test.js.

// Function to perform Multiplication in JavaScript
function multiply(a, b) {
    return a * b;
}

console.log(multiply(5, 10));

Save your file and follow the below steps:

1. Open terminal or command prompt.
2. Set path to where the file is located using the cd command
3. Type “node Test.js” and hit enter(Without quotes).

Now you would see the output in the terminal as 50. This is how we run the JavaScript file through the terminal. Isn’t it very easy? So next time you can test your JavaScript code without running the browser.

Leave a Reply