How to use process.argv In Javascript

In this approach, we are using process.argv, the script retrieves command-line arguments provided when executing the program. The code assigns the third element (process.argv[2]) to the variable input, and it checks whether a value is provided. If a value is present, it prints “Your Name is: [input]”, otherwise, it prints ‘No input provided.’

Syntax:

const arg1 = process.argv[2];

Example: The below example uses process.argv to take input in JavaScript in VS Code Terminal.

Javascript




const input = process.argv[2];
if (input) {
    console.log(`Your Name is: ${input}`);
} else {
    console.log('No input provided.');
}


Output:

How to Take Input in JavaScript in VS Code Terminal ?

In JavaScript, capturing the user input through VScode Terminal can be done using various methods and approaches. These approaches are readline module, process.stdin, and process.argv.

Table of Content

  • Using readline module
  • Using process.argv
  • Using process.stdin

Similar Reads

Using readline module

In this approach, we are using the readline module to create an interface for reading input from the standard input (keyboard) in the VS Code Terminal. The ` rl.question` method prompts the user with ‘Enter Your Name:’, waits for their input, and executes a callback function to display the entered name....

Using process.argv

...

Using process.stdin

In this approach, we are using process.argv, the script retrieves command-line arguments provided when executing the program. The code assigns the third element (process.argv[2]) to the variable input, and it checks whether a value is provided. If a value is present, it prints “Your Name is: [input]”, otherwise, it prints ‘No input provided.’...