How to do prompt


How is it I can do a simple prompt and assign the answer to a variable? For example, to do a prompt to ask someones name, and then use that variable of their name in a string interpolation and say Hello, how are you ___?


prompt() is a function which returns user input in the form of a string. We need to give it a variable to receive the return.

var user = prompt("Enter some text");

We now have the user input stored so we can access the string.


prompt() is a function call expression, and as such can be written anywhere an expression can be written, so interpolation is fully in its wheelhouse.

console.log(`Hello, how are you ${prompt("Enter your first name...")}?`);

That is the ES6 and above form of template literal.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.