A prompt box is typically used when you need the user to provide an input before proceeding.
When a prompt box appears, the user must click either “OK” or “Cancel” after entering a value.
Clicking “OK” returns the entered value, while clicking “Cancel” returns null.
window.prompt(“sometext“,”defaultText“); |
The prompt()
method can be used without the window
prefix.
let person = prompt(“Please enter your name”, “Harry Potter”); let text; if (person == null || person == “”) { text = “User cancelled the prompt.”; } else { text = “Hello “ + person + “! How are you today?”; } |
To insert line breaks inside a popup box, use a backslash followed by the letter ‘n’.
alert(“Hello\nHow are you?”); |