Lesson 6 of 30 – JavaScript Popup
20%

JavaScript Popup

JavaScript provides built-in popup boxes to communicate with users. These popup boxes can display messages, ask for confirmation, or collect user input.

Note: JavaScript popup functions are global functions and can also be called using the window object. Example: window.alert(); window.confirm(); window.prompt();
JavaScript Message Boxes

JavaScript provides three built-in popup methods.

Method Purpose
alert() Displays a message with an OK button.
confirm() Displays OK and Cancel buttons.
prompt() Takes input from the user.
alert()

The alert() function displays information to the user.

It contains only one button: OK.

Examples:
alert("Welcome to Soopro Pathshala");

alert(200);

alert("Number: " + 200);

alert(Date());
Output:
A popup message appears with an OK button.
confirm()

The confirm() function asks the user to confirm an action.

It displays two buttons:

  • OK
  • Cancel

It returns:

  • true for OK
  • false for Cancel
Example:
if(confirm("Do you want to save?")){
document.write("Saved");
}
else{
document.write("Cancelled");
}
Advantages of JavaScript Popups
  • Easy to use.
  • Built into JavaScript.
  • No extra libraries required.
  • Helps communicate with users.
  • Can collect user input.
  • Useful for confirmations.
  • Suitable for simple notifications.
Key Points
  • alert() displays a message.
  • confirm() asks for confirmation.
  • prompt() accepts user input.
  • alert() has only an OK button.
  • confirm() returns true or false.
  • prompt() returns text or null.
  • Custom popups provide better user experience.
  • Bootstrap Modals are modern popup alternatives.
Best Practices
  • Use alert() for simple messages.
  • Use confirm() before important actions.
  • Use prompt() for small user inputs.
  • Avoid excessive popup messages.
  • Prefer custom modals for professional websites.
  • Make popup messages short and clear.

🧠 Quick Quiz

Which JavaScript function is used to get input from the user?