The prompt method in JavaScript displays a dialog box that prompts the user for input, allowing the end user to enter text as an answer to a question when the page loads.
prompt Method: The prompt method displays a dialog box with an optional message prompting the user to input some text. It returns the text entered by the user or null if the user cancels the dialog.
Usage Example:
let userInput = prompt("Please enter your name:", "Harry Potter");
console.log(userInput);
In this example, a prompt dialog asks the user to enter their name, and the entered text is logged to the console.
References:
MDN Web Docs on prompt
W3C HTML Specification on Dialogs
Contribute your Thoughts:
Chosen Answer:
This is a voting comment (?). You can switch to a simple comment. It is better to Upvote an existing comment if you don't have anything to add.
Submit