How to Take Input in JavaScript in VS Code Terminal ?
Last Updated :
04 Mar, 2024
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.
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.
Syntax:;l
const readline = require('readline');
Example: The below example uses a readline module to take input in JavaScript in VS Code Terminal.
JavaScript
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter Your Name: ', (input) => {
console.log(`Your Name is : ${input}`);
rl.close();
});
Output:

Using process.argv
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:

Using process.stdin
In this approach, we are using process.stdin, the script uses process.stdout.write to prompt the user with 'Enter Your Name:' and listens for data events on process.stdin to capture the user's input. The entered data is then processed, trimmed of leading/trailing whitespace, and displayed as "Your Name is: [input]". Finally, the script exits the process.
Syntax:
process.stdin.on();
Example: The below example uses process.stdin to take input in JavaScript in VS Code Terminal.
JavaScript
process.stdout.write('Enter Your Name: ');
process.stdin.on('data', (data) => {
const input = data.toString().trim();
console.log(`Your Name is : ${input}`);
process.exit();
});
Output:

Similar Reads
How to take user input in JavaScript? Interacting with users is the best way to create engaging and dynamic web applications. Taking user input allows your applications to be interactive and responsive. Here we will see the approach to take user input in JavaScript, specifically using the prompt method, which is perfect for beginners.Ap
3 min read
How do you Run JavaScript Through the Terminal? 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 terminal1. Running JavaScript Directly in the Terminal (REPL Mode)Once Node.js is install
2 min read
How to add code input in React JS? In this article, we are going to learn how we can add Code Input in React JS. React is a front-end JavaScript library for constructing user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. Approach to add code input: To incorporate our
2 min read
Bash Scripting - How to Run Bash Scripting in Terminal In this article, we are going to see how to run bash script in terminal. For example, we create a bash file set of instructions or commands ( known as bash script ), and through command, we can run this file in terminal. Let see how to run bash script in terminal. Example 1 : In this example we prin
2 min read
How to Run JavaScript in Visual Studio? To run JavaScript in Visual Studio, you can either use Node.js in the Terminal or the Code Runner extension. Both methods allow you to execute JavaScript code easily and efficiently within the Visual Studio environment.Using Node.js in TerminalNode.js is a JavaScript runtime that allows you to execu
2 min read
How to Install Node & Run NPM in VS Code? Node.js is an open-source, server-side JavaScript runtime environment built on the V8 engine. It allows developers to execute JavaScript code outside of a web browser. In this article, we will see how to install Node.js and NPM and install packages in VS Code using NPM. Steps to Install NodeJS and N
2 min read