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

Alex Flores
Alex Flores
1,474 Points

Why WITHIN the brackets are there single quotes, a plus sign, the variable and another plus sign?

Is this some form of syntax we haven't covered yet that is in actuality telling JavaScript that what is within these quotes is a variable not a string?

7 Answers

-- --
-- --
12,382 Points

Hi Alex,

That's called concatenating - I'm pretty sure that was covered earlier in the course, but I'm not sure. Here's an explanation of the code, though:

Concatenating basically means linking 'things' together (in this case, strings and variables). That's what the + does in JavaScript - it's used to concatenate things.

Here's the piece of code from the video:

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

... which would output [x questions left] (exactly like that, with the brackets, because the brackets are strings - notice how they are placed between single quotes).

So, in the piece of code above, there are two strings and a variable stored into the variable questionsLeft as its value. questions is the variable. The first string is "[" and the second string is "questions left]". So, in this piece of code, you're concatenating these two strings and the variable questions.

I hope this answers your question! :)

Alex Flores
Alex Flores
1,474 Points

ohhhhhh lol I was totally misreading the quotes! Thought I had it but it was just a bit of magic eye happening for me. Got it now, very much appreciate the help!

-- --
-- --
12,382 Points

No problem! :) Glad I could help.

As did I, this was very confusing. Thanks Alex for the questions and thanks to Mace Lytsman Piernbaum for the clarifying answer - very helpful. I believe the choice of variables (questions and questionsLeft) and the statement _____ questions left only enhance the confusion. Perhaps different variable names or different words in the statement would prevent confusion.

thanks Mace Lytsman Piernbaum for clarifying.

-- --
-- --
12,382 Points

You're welcome! :)

Alex Flores
Alex Flores
1,474 Points

Yes thank you for your help Mace :) btw, how did you get the colors to change on your individual pieces of code to isolate the quotes? Is that possible in Sublime text editor (which is what I'm guessing you're using)?

-- --
-- --
12,382 Points

Do you mean the code in my answer above? Refer to the Markdown Cheatsheet to do that on the Treehouse Community forums. I am pretty sure it is possible in Sublime, but I use Atom to write my code. I'm not sure if it colours the syntax automatically or if it's the syntax theme I'm using right now (Atom Monokai), but for me, all syntax is clearly distinguished in Atom.

Alex Flores
Alex Flores
1,474 Points

gotcha thanks again!

-- --
-- --
12,382 Points

You're welcome. :)

Alex Flores
Alex Flores
1,474 Points

no problem Steve, glad my question could help someone :)

Viktor Léhner
Viktor Léhner
12,771 Points

Thanks, it was a bit counfusing using bracket here....