Beginner
cliweb
Input Prompts
Prompts are used to ask the user for input or feedback on actions.
The most basic way to interact with the user is by alerting them, and waiting for them to acknowledge by pressing [Enter].
!--frsh-copybutton:1-->
alert("Please acknowledge the message.");
console.log("The message has been acknowledged.");
Instead of just an acknowledgement, we can also ask the user for a yes/no response.
!--frsh-copybutton:2-->
const shouldProceed = confirm("Do you want to proceed?");
console.log("Should proceed?", shouldProceed);
We can also prompt the user for some text input. If the user cancels the prompt, the returned value will be `null`.
!--frsh-copybutton:3-->
const name = prompt("Please enter your name:");
console.log("Name:", name);
When prompting you can also specify a default value to use if the user cancels the prompt.
!--frsh-copybutton:4-->
const age = prompt("Please enter your age:", "18");
console.log("Age:", age);
Run this example locally using the Deno CLI:
deno run https://byexample-wwv03xf36j0g.deno.dev/prompts.ts
Additional resources: