Open In App

What is the use of the Prompt Function in JavaScript ?

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The prompt function in JavaScript is used to interact with the user by displaying a dialog box that allows the user to type input text. It is often used to gather user input simply.

Example: Here, the prompt function displays a dialog box with the message “Please enter your name:” and an input field. The user can enter text, and the entered value is stored in the userInput variable. The script then prints a greeting using the entered name.

Javascript




let userInput = prompt("Please enter your name:");
 
console.log("Hello, " + userInput + "!");



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads