How to use Prompt function in JavaScript ? with proper example

Posted by

here is how to use the prompt() function in JavaScript:

The prompt() function is a built-in JavaScript function that displays a dialog box that prompts the user for input. The function takes two arguments:

  • The first argument is a string that contains the message to display to the user.
  • The second argument is a string that contains the default value to display in the text input field.

The function returns a string containing the text that the user entered, or null if the user clicked the Cancel button.

Here is an example of how to use the prompt() function:

JavaScript

let name = prompt("What is your name?");

console.log("Your name is: " + name);

Use code with caution. Learn morecontent_copy

This code will display a dialog box that prompts the user for their name. The default value in the text input field will be empty. When the user clicks the OK button, the function will return the user’s name as a string. The code will then log the user’s name to the console.

OutPut:

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x