Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables Combining Strings

Don Miller
Don Miller
3,582 Points

var visitor = prompt (slight confusion)

When you initially use:
var visitor = prompt("What is your name?");

...and then later down the code you use:
var message = "Hello " + visitor .....

How and/or why does the prompt not appear again? Is this because the prompt already appeared, someone entered their name and now var visitor will always be registered as the person's name (unless changed further down the code).

That's my only confusion right now. Why doesn't the prompt box continue to appear when using var visitor when the purpose of var visitor is literally to prompt? How does it know to stop prompting, I guess that's my question. lol

If I missed this in the video, that I've watched twice, please just tell me to watch the video again. :o)


No problem!

2 Answers

Hey Donald! Good questions, and the confusion is understandable. At this stage of the game, don't worry too much about this piece not making a lot of sense. I think the point is to understand that you can store a users input from the prompt() method in a variable and then use it later in your code. The reason it doesn't prompt again is because the prompt method, on it's own in the global scope of the program, only runs once on page load. You will start learning about loops and functions later on where you can prompt multiple times and even call a function to prompt a user multiple times throughout the code.

Don Miller
Don Miller
3,582 Points

This makes a lot of sense now. Thank you so much for the explanation!!

Jennifer Mitchell
Jennifer Mitchell
Full Stack JavaScript Techdegree Student 3,917 Points

This confused me at first, too! Then I learned the prompt method returns whatever is typed into the box. So whatever is typed gets stored in the variable. Then the program moves on and runs the next line.