Open In App

How do you Run JavaScript Through the Terminal?

Last Updated : 10 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Running JavaScript through the terminal can be done in a few different ways, depending on your environment. Here are the most common methods:

Note- First you need to install Node.js to run JavaScript through the terminal

1. Running JavaScript Directly in the Terminal (REPL Mode)

Once Node.js is installed, you can run the REPL (Read-Eval-Print Loop) — an interactive shell that allows you to execute JavaScript commands line by line.

Step 1: Open your terminal and type node to enter the Node.js REPL (Read-Eval-Print Loop) environment.

node

You can now type JavaScript code directly and execute it.

JavaScript
const add = (a, b) => {
    return a + b
}
console.log(add(4, 6))

Output

Output
Run JavaScript through the Terminal

2. Running JavaScript from a File

Step 1: Create a JavaScript file and add some JS code in it.

JavaScript
// Function to add two variables.

const add = (a, b) => {
    return a + b
}
console.log(add(4, 6))

Step 2: Run the JS file in the terminal using the following command

node fileName.js

Output

Output
Run JavaScript Through the Terminal

3. Run the code in browser's console

You can also run your JavaScript code in your browser console

Step 1: Open Developer Tools, In most browsers (like Chrome, Firefox, or Edge), you can open the developer tools by pressing F12 or Ctrl+Shift+I.

Step 2: Click on the "Console" tab to open console window.

Step 3: You can type JavaScript code directly into the console and see the results immediately.

Output
Run JavaScript Through Terminal

Conclusion

Running JavaScript through the terminal is straightforward and can be done using Node.js, a browser's developer console. Node.js is the most commonly used method, especially for server-side or standalone JavaScript applications. For quick testing or debugging, the browser's developer console or the Node.js REPL is also very handy.

JavaScript is best known for web page development but is also used in various non-browser environments. You can learn about JavaScript from the ground up by following JavaScript Tutorial and JavaScript Examples.


Next Article

Similar Reads