There may be times when we need to get input from the user in our code e.g. sometimes we may need the name of the user.
A prompt box, when popped up, contains two buttons "OK" and "Cancel", along with an input field for the user to enter input.
The basic syntax for popping up a prompt box follows:
prompt("Message Text ", "defaultvalue");
The following example pops up a prompt box asking user to enter his / her name. After the user clicks on the "OK" button the value in the input filed is displayed in an alert box:
The above line of code opens / pops up a dialog box having an input field with the "defaultvalue" already entered and two buttons i.e. "OK" and "Cancel". If the user clicks the "OK" button the prompt() returns the value entered in the input field. Otherwise the prompt() returns null.
var name = prompt("Enter Your name", "Hello World");