How can I Execute JavaScript at the Command Prompt?

To execute JavaScript at the command prompt, you can use several methods depending on your environment. This article covers three practical approaches: running JavaScript files through Node.js, executing code directly in the Node.js REPL, and using the browser console for quick testing.

In this article, we'll explore how to execute JavaScript at the command prompt using different methods.

Methods to Run JavaScript at Command Line

Here are three methods to execute JavaScript at the command prompt, each with detailed explanations and examples:

Running JavaScript Files with Node.js

This method allows you to execute JavaScript files (.js) directly from the terminal using Node.js.

Prerequisites

  • Install Node.js from the official website
  • Create a JavaScript file with your code
  • Open terminal/command prompt in the file's directory

Steps

  1. Navigate to your file's folder using cd command
  2. Run the command: node filename.js

Example

Create a file named example.js with the following content:

function greetUser() {
    let message = "Hello World!!! ";
    let description = "Running JavaScript through Node.js...";
    console.log(message + description);
}

// Calculate and display sum
let num1 = 15;
let num2 = 25;
console.log("Sum:", num1 + num2);

greetUser();

Run with: node example.js

Sum: 40
Hello World!!! Running JavaScript through Node.js...

Using Node.js REPL (Interactive Mode)

The Node.js REPL (Read-Eval-Print Loop) allows you to execute JavaScript code interactively in the terminal.

Steps

  1. Open terminal and type node
  2. You'll enter interactive mode (you'll see > prompt)
  3. Type JavaScript code line by line
  4. For multi-line code, use .editor mode
  5. Press Ctrl+D to exit editor mode or Ctrl+C twice to exit REPL

Example

// In Node.js REPL:
let a = 10;
let b = 20;
let result = a + b;
console.log("Result:", result);

// Using .editor for multi-line code
function calculateArea(radius) {
    return Math.PI * radius * radius;
}

console.log("Circle area:", calculateArea(5));
Result: 30
Circle area: 78.53981633974483

Using Browser Console

Browser console provides a quick way to test JavaScript code without setting up files.

Steps

  1. Open any web browser
  2. Press F12 or Ctrl+Shift+I to open Developer Tools
  3. Navigate to the "Console" tab
  4. Type JavaScript code and press Enter

Example

<script>
    // Browser console examples
    console.log("Hello World");
    console.log("Welcome to TutorialsPoint");
    
    // Math operations
    let x = 5;
    let y = 3;
    console.log("Multiplication:", x * y);
    
    // DOM manipulation (browser only)
    document.body.style.backgroundColor = "lightblue";
</script>

Comparison of Methods

Method Best For Requires Installation File Needed
Node.js File Execution Production code, scripts Yes (Node.js) Yes
Node.js REPL Testing, learning, debugging Yes (Node.js) No
Browser Console Quick testing, DOM manipulation No No

Key Points

  • Node.js is required for terminal-based JavaScript execution
  • Use console.log() for output in Node.js environment
  • Browser console is ideal for DOM-related testing
  • REPL mode is perfect for interactive coding and experimentation

Conclusion

Each method serves different purposes: Node.js for server-side scripts, REPL for interactive testing, and browser console for client-side debugging. Choose the method that best fits your development needs and environment.

Updated on: 2026-03-15T22:20:07+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements