How to make an input from the keyboard in JavaScript

Learn how to take user input in JavaScript with an example. Get user input from the keyboard and use it to control your script.

Making an Input from the Keyboard in JavaScript

Getting user input from the keyboard is a critical part of almost any programming language. In JavaScript, the prompt() function is used to make an input from the keyboard. This function takes a string parameter that is the text that is prompted to the user. The following example shows how to use the prompt() function to get a user input:


// Get user input
let username = prompt("Please enter your username:");
console.log(username);

In the above example, the program will display the prompt "Please enter your username:" to the user. The user will then enter their username, which will be stored in the username variable.

The prompt() function can also take a second parameter that is the default value of the input field. This can be useful if you want to pre-fill an input field with a default value. For example:


// Get user input
let username = prompt("Please enter your username:", "default_username");
console.log(username);

In the above example, the program will display the prompt "Please enter your username:" to the user, with the input field already filled with the text "default_username". The user can then enter their username, which will be stored in the username variable.

The prompt() function can also take a third parameter that is a boolean value indicating if the input should be hidden or not. If the third parameter is set to true, the user will not be able to see what they are entering in the input field. For example:


// Get user input
let username = prompt("Please enter your username:", "default_username", true);
console.log(username);

In the above example, the program will display the prompt "Please enter your username:" to the user, with the input field already filled with the text "default_username", but the user will not be able to see what they are entering in the input field. The user's input will be stored in the username variable.

The prompt() function returns a string value containing the user's input. However, if the user clicks the "Cancel" button, the prompt() function will return null. Therefore, if you want to make sure that the user has actually entered something, you should check if the return value is not null:


// Get user input
let username = prompt("Please enter your username:", "default_username");

if (username !== null) {
  console.log(username);
} else {
  console.log("User cancelled the prompt.");
}

In the above example, the program will display the prompt "Please enter your username:" to the user, with the input field already filled with the text "default_username". If the user clicks the "OK" button, the value of the user's input will be stored in the username variable and logged to the console. If the user clicks the "Cancel" button, the username variable will be set to null and a message will be logged to the console.

The prompt() function can be used to get input from the keyboard in JavaScript. It takes a string parameter that is the text that is prompted to the user, and optionally takes a second and third parameter that is the default value of the input field and a boolean value indicating if the input should be hidden or not, respectively. The prompt() function returns a string value containing the user's input, or null if the user clicks the "Cancel" button.

Answers (0)