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

TayZar Soe
TayZar Soe
1,695 Points

I don't quite understand the quotation marks here.

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

Above is the placement of the quotation marks according to the instructor. I get that strings are meant to be left inside quotes and numbers don't have any quotes. What's the purpose of the single quotation mark immediately after opening bracket. Why don't we need closing quotation mark after left since it's supposed to be a string.

4 Answers

"Questions" is a variable, I assume that holds a numerical value. That variable is being concatenated with two strings. A string starts with a quotation, and ends at the next quotation; which is where you're being confused here. The opening bracket is a string of its own, it's concatenated with whatever value "questions" holds, which is then concatenated with the second string, ' questions left]'. After concatenation, questions will no longer be an expression, but rather one long string value.

var questions = 5,
questionsLeft = '[' + questions + ' questions left]';
console.log(questionsLeft); //this would return the string "[5 questions left]"

It may be more intuitive seeing it with string interpolation

var questions = 5,
questionsLeft = `[${questions} questions left]`;
console.log(questionsLeft); //this would return the string "[5 questions left]" as well

Another way to look at it could be to imagine the types

//Note: not actual code to be ran, just an example
var questions = Number,
questionsLeft = String + Number + String;
console.log(questionsLeft); //returns String
TayZar Soe
TayZar Soe
1,695 Points

That helped out a lot. I failed to realize that the brackets are meant to be a string; hence, the opening bracket was inside quotation marks. Thank you for your help. Cheers.

Very helpful. Thanks Ricardo.

The brackets threw me off too.

thank you Ricardo!!

Rebekah McCarley
Rebekah McCarley
1,835 Points

THANK YOU. That was really throwing me off as well.

Oh I got it now. Same here that I didn't recognize that a bracket [ is a string. Thanks!

Honza Noel
Honza Noel
2,779 Points

I get it now!! Love that I'm not the only one confused! Was just about to pack it all in. Thanks.

Lia Seltene
Lia Seltene
2,641 Points

Christ thank you ;_; You, Mace, and andren, saved my butt here.