How to use the prompt function from readline-sync

Find comprehensive JavaScript readline-sync.prompt code examples handpicked from public code repositorys.

readline-sync.prompt is a function in Node.js that allows the user to enter text input into the command line and returns the entered text as a string.

181
182
183
184
185
186
187
188
189
190
```

### <a name="basic_methods-prompt"></a>`prompt`

```js
input = readlineSync.prompt([options])
```

Display a prompt-sign (see [`prompt`](#basic_options-prompt) option) to the user, and then return the input from the user after it has been typed and the Enter key was pressed.  
You can specify an `options` (see [Basic Options](#basic_options)) to control the behavior (e.g. refusing unexpected input, avoiding trimming white spaces, etc.).
fork icon61
star icon1
watch icon2

+ 31 other calls in file

-3
fork icon0
star icon0
watch icon1

+ 31 other calls in file

How does readline-sync.prompt work?

readline-sync.prompt works by reading input from the user via the command line interface in Node.js, prompting the user to enter text input and then returning the entered text as a string.

When readline-sync.prompt is called, it displays a message to the user via the command line interface, prompting them to enter input. The function then waits for the user to enter text input, which is typically done via the keyboard.

Once the user has entered input, readline-sync.prompt captures the text input and returns it as a string, which can then be used in the program as needed.

readline-sync.prompt is a synchronous function, which means that it blocks further execution of the program until the user has entered input and the function has returned the entered text as a string. This can be useful in situations where user input is required before the program can continue executing.

-3
fork icon0
star icon0
watch icon1

+ 17 other calls in file

785
786
787
788
789
790
791
792
793
794
795


//Contains all the prompts used within the game logic.
let prompts = {    
    //Used for obtaining a username. Allows only alphanumeric characters up to 15 long.
    formattedNamePrompt(){
        let answer = readlineSync.prompt()
        if(/^[a-zA-Z0-9]{1,15}$/.test(answer)){
            return answer;
        }else{
            return false;
fork icon0
star icon0
watch icon0

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
const readlineSync = require("readline-sync");

// Prompt the user to enter their name
const name = readlineSync.prompt("What is your name? ");

// Greet the user using the entered name
console.log(`Hello, ${name}!`);

In this example, we first import the readline-sync module, which provides functions for reading input from the command line interface in Node.js. We then call readlineSync.prompt('What is your name? '), which displays the prompt message "What is your name?" to the user and waits for them to enter text input via the keyboard. Once the user has entered their name, readlineSync.prompt returns the entered text as a string, which we store in a variable named name. Finally, we use the entered name in a greeting message using string interpolation, and log the message to the console.