Communicating with the User

The browser provides three built-in windows for communication. They are simple but have their quirks.

1. alert() — The Megaphone

Shows a message and stops code execution until the user clicks " OK " .

alert("You have a new message!");

2. prompt() — The Questionnaire

Asks a question and returns the entered text.

let age = prompt("How old are you?", 18); // 18 is the default value

Important: prompt always returns a string. If the user enters 25, you get "25".

3. confirm() — The Judge

Asks a question with two buttons: " OK " (returns true) and " Cancel " (returns false).

let isAdmin = confirm("Are you an administrator?");
Warning

These windows block (freeze) the entire page. You can't even scroll the site while the window is open. Therefore, they are rarely used in modern UX, being replaced by " pretty " modal windows.

Note

If the user clicks " Cancel " in prompt, the function returns null.