# Prompt, Alert, Confirm

## Prompt

**Javascript** provides a simple way to collect input from the user - the `prompt()` function:

```javascript
// Input must be stored in a variable or else it goes nowhere!
let userResponse = prompt("What is your name?");
```

The [**`prompt`**](https://www.w3schools.com/jsref/met_win_prompt.asp) command has two arguments. *`prompt(text, [defaultText])`* and returns a `string` of what the user entered. *It is important to note, that it returns a string*.

{% embed url="<https://repl.it/repls/CompetitiveDistantBackup>" %}

If the user clicks **cancel**, then the `prompt()` function returns `null`.

## Alert

The [**`console`**](https://cs.brash.ca/unit-2/input-output/the-console) is a great way to give output as a developer (testing, errors, warnings) but general users do not *see* the console window! If you want to give a quick pop-up of information to the user (without any input from the user), you can use the `alert()` function.

![General Alert window with OK button](https://1200419583-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LKbpNQDFNJap9OTDSt6%2F-LRdZgCEOhROaLX7Hm7A%2F-LRdad001ZwV8b7Yu03j%2FAlert.png?alt=media\&token=fab799ea-5c68-4f60-8d17-f3695e268e9c)

The `alert()` function is very simple. It takes a string and returns nothing. To use it, you just call it with whatever you want shown in the pop-up box.

```javascript
alert("The sky is falling, the sky is falling!");
```

## Confirm

Maybe you don't want text from the user but rather a YES or NO (or perhaps `OK` or `CANCEL`). Javascript provides the `confirm()` function to ask the user a question and present them with two choices that equate to `true` or `false`.

![Press OK for yes, CANCEL for no](https://1200419583-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LKbpNQDFNJap9OTDSt6%2F-LRdZgCEOhROaLX7Hm7A%2F-LRdcIZ7KSfMBrw_InSA%2FConfirm.png?alt=media\&token=01da7c80-36e7-419a-ae78-0487e2e5bf0e)

The ability to retrieve a boolean value from the user can be very useful. The `confirm()` function takes a string as input and returns a boolean.

```javascript
let userAnswer = confirm("You will become a zombie");
```

{% hint style="success" %}
While many examples could be provided of the three methods listed, it is much more rewarding if you try them out yourself.
{% endhint %}
