Skip to content
Advertisement

How to store user input using prompt() in JavaScript?

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?

Advertisement

Answer

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);
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement