0

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?

4
  • You can assign it to a variable. What exactly do you need to do? Commented Aug 27, 2022 at 22:54
  • 2
    How much research effort is expected of Stack Overflow users? You should read the docs: Window.prompt() There is even an example how to use this function. Commented Aug 27, 2022 at 22:58
  • 1
    Some friendly advice, these sorts of questions can usually be solved by a quick Google. Here is the first result from that w3schools.com/jsref/met_win_prompt.asp. The very first example shows an example of what you are looking for. People on Stackoverflow really don't like it when people come with questions that can be easily searched, since it wastes both your and our time. Commented Aug 27, 2022 at 23:00
  • I find it, however, nice to also share answers to basic questions on such a platform so if you're not really interested in reading some docs and want to remind yourself smth - you can specifically ask a question and get a fast and specific answer. If you feel like you're not familiar with the thing you're asking about - then you find resources like those mentioned above. Commented Mar 4, 2023 at 22:44

1 Answer 1

4

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);
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.