Use JavaScript alert(), confirm(), and prompt() and read their return values

alert() shows a message and returns undefined. confirm() returns true or false. prompt() returns the entered string or null when canceled.

Compare the three dialogs and their returns

Function Purpose Return value
alert(message) Show a notice undefined
confirm(message) Accept or cancel Accept: true. Cancel: false.
prompt(message, defaultValue) Enter text Text, or null if canceled.

The browser controls the appearance and can suppress native dialogs. They normally interrupt interaction. A suppressed confirmation can return false and a suppressed prompt can return null. See HTML Living Standard.

Show a message with alert()

Use alert() for a short notice. The return value does not tell you which button was pressed. Prefer a message in the page if it needs to stay visible or include a link.

const result = window.alert('Saved successfully.');
console.log(result); // undefined

Check acceptance or cancellation with confirm()

Check confirm() before continuing with the action. A result of false means the action was not confirmed, whether the user canceled or the browser suppressed the dialog.

const accepted = window.confirm('Apply this setting?');
if (accepted) {
  console.log('Applying the setting.');
} else {
  console.log('Keeping the current setting.');
}

Distinguish text, empty input, and cancellation with prompt()

The second argument to prompt() fills in a default value. Clearing the field and accepting gives an empty string. Canceling gives null. Test for null before trimming.

const answer = window.prompt('Enter your name.', 'Mina');
if (answer === null) {
  console.log('Input canceled.');
} else if (answer.trim() === '') {
  console.log('Enter some text.');
} else {
  console.log(answer.trim());
}

Even digits are returned as a string. Converting null or an empty string directly with Number() can give zero. Handle cancellation first, validate decimal digits, then check the numeric range. This example accepts quantities from 1 to 10.

{
  const answer = window.prompt('Enter a quantity from 1 to 10.', '1');

  if (answer === null) {
    console.log('Quantity entry canceled.');
  } else {
    const text = answer.trim();
    const quantity = Number(text);
    if (/^[0-9]+$/.test(text) && Number.isInteger(quantity) && quantity >= 1 && quantity <= 10) {
      console.log(quantity);
    } else {
      console.log('Enter an integer from 1 to 10.');
    }
  }
}

Run the example in a browser console. Try 3, an empty input, 1e1, and cancel. In these trials, only the input 3 is logged as a number. Empty input and exponent notation get an error message, and cancellation gets its own message.

Try all three in a complete example

Save the HTML as browser-dialogs.html in UTF-8 and open it. The buttons display the results in the page after the dialog closes. Try accepting and canceling confirmation, then entering text, clearing the field, and canceling the prompt.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Practice browser dialogs</title>
</head>
<body>
<button id="show-alert" type="button">Open alert</button>
<button id="show-confirm" type="button">Open confirm</button>
<button id="show-prompt" type="button">Open prompt</button>
<p id="output" aria-live="polite">Choose a button to see the return value.</p>
<script>
const output = document.querySelector('#output');

document.querySelector('#show-alert').addEventListener('click', () => {
  const result = window.alert('Check the operation.');
  output.textContent = `alert() return value: ${String(result)}`;
});

document.querySelector('#show-confirm').addEventListener('click', () => {
  const accepted = window.confirm('Apply the settings?');
  output.textContent = accepted ? 'confirm(): accepted (true)' : 'confirm(): canceled (false)';
});

document.querySelector('#show-prompt').addEventListener('click', () => {
  const name = window.prompt('Enter your name.', 'Mina');
  output.textContent = name === null
    ? 'prompt(): canceled (null)'
    : `prompt(): ${JSON.stringify(name)}`;
});
</script>
</body>
</html>