You can run JavaScript in the terminal with the help of a tool called NodeJs. NodeJs is a free and open-sourced run-time environment that lets you write JavaScript command-line tools and server-side scripts outside of a browser.
In this guide, we will create a simple program that will run Hello World on the terminal. But before we can begin, make sure that you have installed NodeJs on your computer.
Step 1: Create a JavaScript file
The first step is to open a text editor of your choice and create a JavaScript file called app.js
.
Step 2: Write a JavaScript code
The second step is to write some JavaScript code inside the app.js
file. All you have to do is add the console.log
method, and the value be “Hello World!”
console.log("Hello World!")
This code will simply output the message “Hello World!” when you execute the code.
Step 3: Run the JavaScript code
The third and last step is to run the code that you just wrote. To do that, open the terminal and set the path to the location of your JavaScript file.
Next, write node app.js
and press enter key to execute the JavaScript file:
node app.js
If you see the message “Hello World!” - you have successfully run your JavaScript code in the terminal!
I hope you find this guide helpful and happy coding.
Get my free e-book to prepare for the technical interview or start to Learn Full-Stack JavaScript