Don’t miss our Skill Seeker Series from Sept 30 – Oct 30.

Don’t miss our Skill Seeker Series from Sept 30 – Oct 30.

COMING SOON: Cybersecurity. Join the waitlist for an exclusive IT & Cybersecurity Workshop discount.

ALERT: We’re offering creatives and marketers next-gen skills for FREE, in partnership with Adobe. Do you qualify?

← Back to the blog

Article

How to Easily Run JavaScript in Terminal

General Assembly
March 15, 2021
Learn How To Run JavaScript in a Terminal

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:

  1. Client-Side JavaScript 
  2. Server-Side JavaScript
  3. 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:

  1. Installing Node.js.
  2. Accessing Node.js in Terminal/Command Prompt.
  3. Running your JS file using node.

Installing Node.js

  1. Go to https://nodejs.org/en/download/; you should see a web page like below:
Screenshot of the node.js website. Node is a key tool to run JavaScript in your terminal.
  1. If you are using Windows OS, click on Windows Installer or else click on Mac Installer for macOS.
  2. 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:

  1. Open the Spotlight Search Bar (Cmd+Space bar).
  2. Type Terminal: it has an icon like below — open it.
  3. 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

  1. Create a new file called index.js in your Desktop/folder
  2. 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

  1. Go to “Desktop path” within your Terminal/Command-Prompt:
cd /Users/arwalokhandwala/Desktop/
  1. To run your JavaScript file using Node.js, type:
node index.js
  1. 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.

LET’S CONNECT

What’s your reason for connecting? *

By providing your email, you confirm you have read and acknowledge General Assembly’s Privacy Policy and Terms of Service.