avaScript Simple Actions: alert()
, prompt()
, confirm()
JavaScript provides three built-in functions for simple user interactions:
1️⃣ alert()
→ Display a message
2️⃣ prompt()
→ Get user input
3️⃣ confirm()
→ Ask for confirmation
1️⃣ alert()
– Display a Message
- Used to show a simple pop-up with a message.
- The user cannot provide input; they can only click "OK" to close the alert.
Example:
✅ Use Case:
- To show an important notification or warning.
2️⃣ prompt()
– Get User Input
- Displays a pop-up with a text input field.
- The user can enter a value and click "OK" or "Cancel".
- Returns the entered value (as a string) or
null
if the user clicks "Cancel".
Example:
✅ Use Case:
- To collect user input, like names, numbers, or preferences.
š¹ Handling Default Values:
š¹ Checking if Input is null
(User Clicked Cancel):
3️⃣ confirm()
– Ask for Confirmation
- Displays a pop-up with "OK" and "Cancel" buttons.
- Returns
true
if the user clicks "OK", otherwise returnsfalse
.
Example:
✅ Use Case:
- To confirm delete actions, logouts, or other important decisions.
šÆ Summary
Function | Purpose | Return Value |
---|---|---|
alert() | Displays a message | Nothing (undefined ) |
prompt() | Asks for user input | User input as a string , or null if canceled |
confirm() | Asks for confirmation | true if OK, false if Cancel |
š Now you're ready to interact with users in JavaScript! Let me know if you need more details. š