How do you Run JavaScript Through the Terminal?
Last Updated :
12 Jul, 2025
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
Run JavaScript through the Terminal2. 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
Run JavaScript Through the Terminal3. 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.
Run JavaScript Through TerminalConclusion
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.
How do you run JavaScript script through the Terminal?
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics