TL;DR
You can run JavaScript console in terminal or any command-line interface using Node.js, an open-source, platform-agnostic runtime that executes JavaScript outside a web browser.
Before we take a deep dive into how to run JavaScript in browser, we need to understand few basic terminologies like:
- Client-Side JavaScript
- Server-Side JavaScript
- Command Line Interface
Client-Side JavaScript
- JavaScript code executed in the web browser is known as client-side JavaScript.
- Client-side JS was originally used to add some interactivity on websites; for example, the Click on Submit button in a form sends form details to the server.
- The <script> tag within your HTML page is used to write client-side JavScript, which is later executed by the browser.
<script>
console.log("Client-side JavaScript");
</script>
Server-Side JavaScript
- When you run JS code outside the browser-like on a web server, it becomes server-side JavaScript.
- Server-side JS is generally used to write the back-end logic of your web application; for instance, you can check to see if a user password matches the stored DB password.
- You can run Server-side JavaScript using any command-line interface.
But, what is Command Line Interface, a.k.a.,Terminal?
- CLI is a text-based interface that allows users to perform some operation in a computer by typing commands.
- The most common CLI for popular OS’s are:
- Windows: Command Prompt, PowerShell
- Mac: Terminal, iTerm
Let’s see how to run JavaScript in these popular CLI’s:
Running JavaScript in Terminal
Executing JavaScript in Terminal has two steps:
- Installing Node.js.
- Accessing Node.js in Terminal/Command Prompt.
- Running your JS file using node.
Installing Node.js
- Go to https://nodejs.org/en/download/; you should see a web page like below:
- If you are using Windows OS, click on Windows Installer or else click on Mac Installer for macOS.
- Once downloaded, double-click on the installer to install Node.js.
Checking Node.js in Your Terminal/Command Prompt
To open your terminal in macOS:
- Open the Spotlight Search Bar (Cmd+Space bar).
- Type Terminal: it has an icon like below — open it.
- Once opened, type the following command:
node -v
If you see an output like this, v14.15.3 Node.js is installed successfully.
Writing Your JS Code
- Create a new file called index.js in your Desktop/folder
- Let’s write some code!
const greet = (name=”Everyone”) => { console.log(`Hello ${name}`);}
greet();
Now, let’s run it!
Running JavaScript in Your Terminal/Command Prompt
- Go to “Desktop path” within your Terminal/Command-Prompt:
cd /Users/arwalokhandwala/Desktop/
- To run your JavaScript file using Node.js, type:
node index.js
- If you see an output like below, then Congratulations! You are successfully running your JavaScript file in your Terminal/Command-Prompt:
Hello Everyone
Passing Runtime Arguments in Node.js
Like in the browser, we use forms to pass custom values to our JavaScript. If you wish to pass runtime values, then you can use process.argv[2]
const greet = (name = "Everyone") => {
console.log(`Hello ${name}`);
}
greet(process.argv[2]);
In your Terminal/Command-prompt, type:
node index.js ArwaHello Arwa
Conclusion
Node.js makes it very simple to run JavaScript code in your Terminal/Command-prompt and opens a door of opportunities for a web developer.