Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How To Run JavaScript In Terminal

In this blog post, we'll learn how to run JavaScript code in the terminal, also known as the command-line interface (CLI). Running JavaScript in this way can help you test and debug your code more efficiently or even create scripts that can make your life easier. The process is quite simple and will be broken down into steps for easy understanding.

Prerequisites

Before we start, you should have basic knowledge of JavaScript and be familiar with using the terminal. If you're new to the terminal, don't worry, we'll briefly cover the basics as well.

Why Run JavaScript in the Terminal?

You might be wondering, "Why would I want to run JavaScript in the terminal when I can just use a web browser?" Well, there are several reasons to do so:

Testing and Debugging: Running JavaScript in the terminal can make it easier to test and debug your code, as you can view console logs and error messages directly in the terminal without having to switch between the browser and your code editor.

Creating Scripts: You can create scripts that automate tasks or perform specific functions, such as file manipulation or server-side processing, using JavaScript in the terminal.

Using Node.js: Node.js is a powerful JavaScript runtime that allows you to run JavaScript on the server-side or create command-line tools using JavaScript. Running JavaScript in the terminal is essential to leverage the full potential of Node.js.

Now that we know why we might want to run JavaScript in the terminal let's see how we can actually do it.

Step 1: Installing Node.js

First, we need to install Node.js, which is the JavaScript runtime that will allow us to run JavaScript code in the terminal. To download Node.js, go to the official Node.js website and download the installer for your operating system (Windows, macOS, or Linux). Follow the instructions on the website to install Node.js.

Once you've installed Node.js, you can verify your installation by opening your terminal and typing the following command:

node -v

This command will display the version number of Node.js installed on your system, like this:

v14.17.0

If you see a version number, you've successfully installed Node.js.

Step 2: Creating a JavaScript File

Next, we need to create a JavaScript file that we'll run in the terminal. Open your favorite code editor and create a new file named hello.js. In this file, add the following JavaScript code:

console.log("Hello, world!");

This simple code snippet will display a message "Hello, world!" when we run it. Save the file and close your code editor.

Step 3: Running the JavaScript File in the Terminal

Now that we have our JavaScript file ready, it's time to run it in the terminal. Open your terminal and navigate to the folder where you saved the hello.js file. You can do this using the cd (change directory) command, followed by the path to the folder. For example:

cd /path/to/your/folder

Replace /path/to/your/folder with the actual path to the folder containing your hello.js file.

Once you're in the correct folder, type the following command to run your JavaScript file:

node hello.js

After you press ENTER, you should see the "Hello, world!" message displayed in the terminal:

Hello, world!

Congratulations! You've successfully run a JavaScript file in the terminal using Node.js.

More Examples

Now that we know how to run JavaScript code in the terminal, let's explore a couple more examples to help solidify the concept.

Example 1: Basic Arithmetic

Let's create a new JavaScript file named arithmetic.js with the following code:

const num1 = 10;
const num2 = 5;

const sum = num1 + num2;
const difference = num1 - num2;
const product = num1 * num2;
const quotient = num1 / num2;

console.log(`Sum: ${sum}`);
console.log(`Difference: ${difference}`);
console.log(`Product: ${product}`);
console.log(`Quotient: ${quotient}`);

To run this file in the terminal, navigate to the folder containing arithmetic.js and type the following command:

node arithmetic.js

You should see the following output:

Sum: 15
Difference: 5
Product: 50
Quotient: 2

Example 2: Reading a File

In this example, we'll use the built-in fs (file system) module in Node.js to read the contents of a text file. First, create a new text file named sample.txt in the same folder as your JavaScript files and add some text to it.

Next, create a new JavaScript file named readFile.js with the following code:

const fs = require("fs");

fs.readFile("sample.txt", "utf8", (err, data) => {
  if (err) {
    console.error("Error reading file:", err);
    return;
  }
  console.log("File contents:", data);
});

To run this file in the terminal, type the following command:

node readFile.js

You should see the contents of the sample.txt file displayed in the terminal:

File contents: This is a sample text file.

Conclusion

In this blog post, we learned how to run JavaScript code in the terminal using Node.js. This skill can be helpful for testing and debugging your code, creating scripts, or working with Node.js applications. By following the simple steps outlined in this post, you can easily run your JavaScript code directly in the terminal and take advantage of the many benefits this approach offers. Happy coding!