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 OverflowHow to store user input using prompt() in JavaScript? - Stack Overflow
Input as number in JavaScript prompt - Stack Overflow
prompt function in JS ? (Eloquent JavaScript book)
How do you display a prompt onto a webpage with JavaScript and HTML?
Videos
» npm install prompt
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!
Sorry if this is a stupid question. Point is, I’ve seen that anything to do with console.log() doesn’t actually log onto the webpage; document.write() does. How would I display something similar to the prompt() method onto the webpage? I want users to be able to input something. Again, sorry for the stupidity, I’m new to JavaScript.