Tentikalz
Tentikalz

Reputation: 9

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?

Upvotes: -1

Views: 3392

Answers (1)

Dan
Dan

Reputation: 152

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

Upvotes: 3

Related Questions