Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Prompt Box

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.

Syntax

window.prompt(“sometext“,”defaultText“);

The prompt() method can be used without the window prefix.

Example

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?”;
}

Line Breaks

To insert line breaks inside a popup box, use a backslash followed by the letter ‘n’.

Example

alert(“Hello\nHow are you?”);