Just assign the function call to a variable and don't forget to perform answer checks

var answer = prompt("What is your favorite animal?");
if (answer != null && answer.length !== 0) {
   // do your stuff here
   alert(answer);
}

Answer from Dan on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Window › prompt
Window: prompt() method - Web APIs | MDN
window.prompt() instructs the browser to display a dialog with an optional message prompting the user to input some text, and to wait until the user either submits the text or cancels the dialog.
🌐
W3Schools
w3schools.com › jsref › met_win_prompt.asp
Window prompt() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST
Discussions

How to store user input using prompt() in JavaScript? - Stack Overflow
If I were to have a prompt in my code like this: prompt("What is your favorite animal?"); Is there a way to store the answer? More on stackoverflow.com
🌐 stackoverflow.com
Input as number in JavaScript prompt - Stack Overflow
Can we customize a prompt box in JavaScript so it takes time as input? var r = prompt("ENTER THE NUMBER OF SECONDS AFTER WHICH YOU WANT TO BE REMINDED"); More on stackoverflow.com
🌐 stackoverflow.com
prompt function in JS ? (Eloquent JavaScript book)
prompt only works in browser. If you're trying to run this in node or such, it won't be found More on reddit.com
🌐 r/learnjavascript
10
1
May 14, 2021
How do you display a prompt onto a webpage with JavaScript and HTML?
In your html create a span or div with an id. You can then use the either getElementById or querySelector with that particular id in your script, which will allow the page to display the text in question. If you need examples W3 schools has some good ones. More on reddit.com
🌐 r/learnprogramming
3
1
July 20, 2022
🌐
Codecademy
codecademy.com › docs › javascript › window › prompt()
JavaScript | window | prompt() | Codecademy
February 20, 2025 - The prompt() function is a built-in JavaScript method that displays a modal dialogue box prompting the user for input. It is part of the Web API, provided by the browser’s window object, and pauses script execution until the user responds.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-window-prompt-method
Javascript Window prompt() Method - GeeksforGeeks
August 5, 2025 - The window.prompt() method in JavaScript is a built-in function that displays a dialog box prompting the user to input some text.
Find elsewhere
🌐
Medium
medium.com › @meghamurari12 › lesson-16-user-input-with-prompt-in-javascript-df46a299ecab
📘 Lesson 16: User Input with prompt() in JavaScript | by Meghamurari | Medium
August 9, 2025 - The prompt() function in JavaScript displays a dialog box that prompts the user for input. It's a simple way to interact with users and…
🌐
npm
npmjs.com › package › prompt
prompt - npm
April 11, 2022 - $ node examples/simple-prompt.js prompt: username: some-user prompt: email: some-user@some-place.org Command-line input received: username: some-user email: some-user@some-place.org
      » npm install prompt
    
Published   Apr 11, 2022
Version   1.3.0
Author   Nodejitsu Inc.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Interaction: alert, prompt, confirm
September 8, 2020 - let age = prompt('How old are you?', 100); alert(`You are ${age} years old!`); // You are 100 years old!
🌐
Stack Overflow
stackoverflow.com › questions › 47649426 › input-as-number-in-javascript-prompt
Input as number in JavaScript prompt - Stack Overflow
Can we customize a prompt box in JavaScript so it takes time as input? var r = prompt("ENTER THE NUMBER OF SECONDS AFTER WHICH YOU WANT TO BE REMINDED");
🌐
SheCodes
shecodes.io › athena › 90671-what-is-prompt-in-javascript-how-to-use-it
[JavaScript] - What is prompt in JavaScript? How to use it? | SheCodes
Learn about the prompt function in JavaScript, its usage, and how to use it to get user input and store it as a string.
🌐
Reddit
reddit.com › r/learnjavascript › prompt function in js ? (eloquent javascript book)
r/learnjavascript on Reddit: prompt function in JS ? (Eloquent JavaScript book)
May 14, 2021 -

Hi there,

I started to teach my self JS via the book " Eloquent JavaScript"

I encounter the use of prompt function in the book, which I understand is suppose to take an input from the user (similar to 'scanf' in C (?))

I'm using Visual Studio Code. When I try to run this code:

let theNumber = Number(prompt("Pick a number"));
if (!Number.isNaN(theNumber)) {
console.log("Your number is the square root of " +
theNumber * theNumber);
}

I'm getting an error message: "ReferenceError: prompt is not defined"

How can I use to "prompt" function in JS ?

Thanks for helping!

🌐
Dummies
dummies.com › article › technology › programming-web-design › javascript › how-to-prompt-the-user-for-input-in-javascript-140912
How to Prompt the User for Input in JavaScript | dummies
July 3, 2025 - To store user‐entered data in a variable, you create a new variable and then follow it with =. You then follow it with the prompt statement. ... It's important to note that a single equal sign (=) in JavaScript is called the assignment operator.
🌐
OpenAI
platform.openai.com › docs › guides › prompt-engineering
Prompt engineering | OpenAI API
Any instructions provided this way will take priority over a prompt in the input parameter. ... 1 2 3 4 5 6 7 8 9 10 11 import OpenAI from "openai"; const client = new OpenAI(); const response = await client.responses.create({ model: "gpt-5", reasoning: { effort: "low" }, instructions: "Talk like a pirate.", input: "Are semicolons optional in JavaScript?", }); console.log(response.output_text);
🌐
TutorialsPoint
tutorialspoint.com › javascript-prompt-example
JavaScript Prompt Example
February 25, 2025 - In this article, we will learn to use the prompt() function in Javascript. The prompt() method in JavaScript allows developers to collect user input through a pop-up dialog box. What is the prompt() Method in JavaScript? The prompt dialog box is very
🌐
Purdue University
cs.purdue.edu › homes › bxd › js10.html
JavaScript Prompt
If you want to know about Prompts, well then this page is for you!
🌐
Flexiple
flexiple.com › javascript › javascript-window-prompt
JavaScript Window prompt() Method - Flexiple
April 23, 2024 - The JavaScript Window prompt() method displays a dialog box that prompts the visitor for input. A prompt dialog box shows a specified message, providing an input field for the user to enter a string of text and allowing for a default answer ...
🌐
W3Schools
w3schools.com › js › tryit.asp
W3Schools Tryit Editor - JavaScript Prompt
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
Medium
medium.com › javascript-in-plain-english › ai-prompts-are-great-but-your-js-basic-always-matter-39d54c253de0
AI Prompts Are Great — But Your JS Basic Always Matter | by Shantun Parmar | JavaScript in Plain English
November 24, 2025 - New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers. ... I know you’re all excited about using A.I. to write code and solve problems so much more quickly than any human ever could. It can be a time-saver — it’s almost like having a prompt assistant to help speed up your work.