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