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) Working With Numbers The Mad Libs Challenge Revisited

Use of single quote (') within a string is a stand in for what? The Mad Libs Exercise

Hi all, I've been reading through all 16 comments on the Mad Libs video section that introduces a countdown for the questions remaining that it prompts users with, but none of the comments address what the single quotes within the string of code are stand-ins for (I assume they're stand-ins). I'm not sure if they are simply a space, or if there's anything more to that. If it's just a space, does one single quote mean 1 space?

var questionsLeft = '[' + questions + ' questions left]';

3 Answers

In javascript strings you can use either single quotes or double quotes. You just need to be consistent (if you start with single quotes you must end with single quotes)

var questionsLeft = '[' + questions + ' questions left]';
// is the equivalent to
var questionsLeft = "[" + questions + " questions left]";

Hope that helps!

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

They aren't actually a stand in for anything. They are encasing the string. As Ryan Huber stated, it doesn't matter if you are using singles or doubles.

In the example you are showing, String Concatenation is being employed, which may be why you think you are seeing the quote as being a stand in.

The first set of quotes is surrounding the square bracket, so that is the start of the string. Then the value of the variable questions will be add (by concatenation) to the string (with no spaces). Finally the last set of quotes encapsulates the final part of the sting (as added with concatenation). It starts with a space, so there will be a space after the variable value and before the word questions.

The final output would be (if questions = 3) "[3 questions left]"

Hope this clears it up. :dizzy:

Michael Kalmykov
Michael Kalmykov
8,919 Points

This syntax doesn't make sense to me.

var questionsLeft = '[' + questions + ' questions left]';

I understand that it adds the variable questions to the string 'questions left' but I don't understand how or why the syntax works with it.

if 'text' is a string and no quotes is a variable

then what is 'text]' also, if the 2 +'s are inside the quotes then shouldn't they show up as text inside the string via the prompt?

Any elaboration would be greatly appreciated