User Avatar

Tuomo Kankaanpää

1y ago

Dialogs are an underrated feature in Javascript.

Here is 3 dialogs that you can use to quickly display information or get user input.

1/6

User Avatar

Tuomo Kankaanpää

1y ago

1. Alert

We can display information to the user with alert() dialog.

It is an easy way to show a dialog for the user based on e.g. button click or when an event happens (like data is fetched).

Example:

alert("Hello world!");

2/6

User Avatar

Tuomo Kankaanpää

1y ago

2. Confirm

Confirm dialog is for getting user's confirmation. It shows a dialog with desired text and two buttons: "OK" & "Cancel".

If user clicks "OK" the function returns true and if "Cancel" the return value is false.

Example:

let isOkay = confirm("Are you sure?");

3/6

User Avatar

Tuomo Kankaanpää

1y ago

3. Prompt

To collect user input e.g. user's name we can use prompt-dialog.

It works similarly as confirm-dialog:

- pass the text as parameter

- displays a text input

- returns the text input value when dialog is closed

Example:

let name = prompt("What is your name?");

4/6

User Avatar

Tuomo Kankaanpää

1y ago

We can also pre-populate the text field by providing second parameter for the function:

Example:

let name = prompt("What is your name?", "Tuomo");

5/6

User Avatar

Tuomo Kankaanpää

1y ago

Use Javascript dialogs for quickly displaying information or getting user input.

alert 💬

confirm 👍/👎

prompt ✏️

6/6

The all-in-one writing platform.

Write, publish everywhere, see what works, and become a better writer - all in one place.

Trusted by 80,000+ writers