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 trialSean Daniel
4,811 PointsWhat exactly are these apostrophes doing in this string?
I understand how you use quotation marks to make strings and do not put them on variables.
var questions = 3;
var questionsLeft = ' [' + questions + ' questions left]';
var adjective = prompt('Please type an adjective' + questionsLeft);`
The prompt: Please type an adjective [3 questions left]
What exactly are these apostrophes doing in this string?
1 Answer
Dylan Glover
2,537 PointsHey Sean,
So in JavaScript, Apostrophes or Quotation marks can be interchanged to create a string, and can sometimes depend on company or personal preferences.
In this function specifically, the apostrophes are there to break the equation between string and variable. So in
var questionsLeft = ' [' + questions + ' questions left]';
The beginning of the string is closed off with apostrophes at:
' ['
The middle of the string is applied from the questions
variable, and the end of the string is closed off and added with:
' questions left]'
All of these added together create the full string of [3 questions left]
.
This one was a little confusing because of the square brackets being included in the prompt, but its all just part of the string output. (these could all be done with quotations instead of apostrophes too)
Hope this helps!
Dylan
Sean Daniel
4,811 PointsSean Daniel
4,811 PointsThank you Dylan! Problem solved.
Michael Darcy
12,025 PointsMichael Darcy
12,025 PointsI gave you a +1. I was completely misreading what the value for the
questionsLeft
variable was doing. It had initially looked like there was a string contained within a string.