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

Prompt() Command

"The prompt command returns a string that is whatever the visitor types into the prompt dialogue box." - Dave McFarland

let randomMaleName = prompt("Choose random male name.") ; let story = "I am a big fan of " + randomMaleName + "." ; document.write(story) ;

When you input Per "Dead" Ohlin as a response to the prompt, there aren't any problems. Why is that? The prompt command returns a string, right?

Clayton Perszyk
Clayton Perszyk
Treehouse Moderator 49,047 Points

Not sure I understand your question. What were you expecting to happen?

1 Answer

Steven Parker
Steven Parker
243,266 Points

You are correct, the "prompt" function returns a string. The rest of the example code combines that string with other parts of a sentence and then writes the combined string in the variable "story" to the page.

I'm a bit confused when you say "...there aren't any problems. Why is that?". It sounds like you are expecting some problem, is that so?

Yes because I learned that you cant have quotation marks inside quotation marks (" " " " <-- not good good --> " ' ' " ) without an escape character ().

Steven Parker
Steven Parker
243,266 Points

@ah11 — I see now how that might have been a surprise. The trick is that the issue about quotes only counts when you are constructing a string literal. The quotes you type in to the prompt go straight into the variable so they don't cause any problem when you concatenate them with the other strings.

Happy coding!