If I were to have a prompt in my code like this:
JavaScript
x
2
1
prompt("What is your favorite animal?");
2
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
JavaScript
1
5
1
var answer = prompt("What is your favorite animal?");
2
if (answer != null && answer.length !== 0) {
3
// do your stuff here
4
alert(answer);
5
}