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) Creating Reusable Code with Functions Giving Information to Functions

Question

I have a question what did the teacher typed in the seventh line? the code will be displayed below

return area + " "+ unit;

What does those double quotes do and does it take an important role those double quotes?

3 Answers

Steven Parker
Steven Parker
231,269 Points

Double quotes can be used to surround a literal string.

In this case, the string is a single space. And it is used to separate area from unit so they won't run together.

Another question. will be there a difference if you didn't you left a space and you you didn't left space. Like is there gonna be a difference if you put 2 spaces 1 space or with no space? etc.

Another question steven, I'm right now complicated the double quotes are spaces right?, Then when we use them?

Steven Parker
Steven Parker
231,269 Points

With no space you would also not need the quotes, or one of the "+" symbols. With two spaces you would just see more separation between the area and the unit. I would need to see more of the code where this is used to be sure, but I suspect the difference would only be visual.

The quotes are not "complicated", they are necessary to surround a string. This is true for any string, whether it contains words or just space. JavaScript treats double (") and single (') quotes the same. You can use either kind, but be sure they match on both sides.

The double quotes used in this fashion is used as a space. It should also be noted that type of that expression will be of type String afterwards. You could write the same expression using ES6 template literals, which may come off as more intuitive; but the empty string method is something you will see in a lot of code:

return `${area} ${unit}1; 
//this will return the value of area and unit separated by a space, the same as:
return area + " " + unit;
//to see the type of this expression you can do something like, or use typeof() in your example code where
//the function will be returned:
console.log(typeof(5 + " " + "sq ft"); //returns type String
Erika Suzuki
Erika Suzuki
20,299 Points

In PHP and Shell, the strings enclosed in single quotation is a string literal. That means what you wrote is what you get. Strings enclosed in double quotations, however, will be parsed. Hence it is advised to use single quotations in PHP or shell to maximize performance.

In JavaScript though and other languages such as Python, single and double quotation are treated the same - as string literal.