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 trialAlex Flores
1,474 PointsWhy 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 PointsHi 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
1,474 Pointsohhhhhh 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 PointsNo problem! :) Glad I could help.
Steve Tenpas
6,234 PointsAs 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.
Balasubramanyam Gurajapu
Courses Plus Student 358 Pointsthanks Mace Lytsman Piernbaum for clarifying.
-- --
12,382 PointsYou're welcome! :)
Alex Flores
1,474 PointsYes 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 PointsDo 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
1,474 Pointsgotcha thanks again!
-- --
12,382 PointsYou're welcome. :)
Alex Flores
1,474 Pointsno problem Steve, glad my question could help someone :)
Viktor Léhner
12,771 PointsThanks, it was a bit counfusing using bracket here....