Learn Dynamic HTML With JS
Lesson 1 - HTML JS Relationship
Lesson 2 - HTML DOM
Lesson 3 - Browser BOM
Lesson 5 - Manipulating CSS
Lesson 6 - HTTP Requests
Lesson 7 - Form Handling
The prompt
method in JavaScript is used to display a dialog box with a message, an input field for the user to enter data, and OK
and Cancel
buttons. Here's how you can use it to take user input:
let name = prompt("Enter your name:"); console.log("Hello, " + name + "!");
When you run this code:
OK
.name
.If the user clicks Cancel
or leaves the input field blank and clicks OK
, the prompt
method will return null
. It's important to handle this case if the input is required.
This method is commonly used when you need to gather user input interactively, such as asking for a name, age, or any other information that the user can provide through text input.