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

' [' + questions + ' questions left]'

Hi,

I do not really understand the reason for the first ' or the structure of this statement really, I understand what it does but not now!

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

I would have expected it to be done like this:

var questionsLeft = " " + questions + " Questions Left"; 

both obviously work but nice to know / understand the first one :)

4 Answers

Tom,

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

After running, would leave the variable questionsLeft to be "[5 questions left]", without the quotes.

var questions = 5;
var questionsLeft = " " + questions + " Questions Left";

Would leave the variable questionsLeft with the value " 5 Questions Left", without the quotes.

Notice, the second code does not have the square brackets. [ and ]. There is no reason for having the [ and ] in the first code. except to display them.

Hope that helps.

Hi Tom,

These two pieces of code are very similar. The first will print a set of literal square brackets with the number of questions remaining like this:

(Assuming there is 1 question remaining)

" [1 questions left]"

The other section of code will print:

" 1 Questions Left"

I hope that clears up any confusion.

Oh I didnt even notice it was printing the [], I was thinking it was some crazy array I have never seen before inside speach marks!

Thanks for that

E P
E P
2,215 Points

yes I had the same confusion because of the brackets! LOL thanks for clearing this up.