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

General Discussion JavaScript Foundations Objects Prototypes: Part 1

Michael Walker
Michael Walker
12,466 Points

Variables + sentences...

Hello all, something just occurred to me in watching the videos. I've noticed that we use these ridiculous spaces everywhere anytime we have to combine a defined variable with quotes.

For example instead of using...

var name = "Michael";
console.log("Hello " + name); <---notice the space after Hello

...why don't we instead use....

var name = "Michael";
console.log("Hello", name);

It's just my observation that the comma inserts the space for you. Is there an advantage to using the plus sign instead of the comma? I'm new to this programming thing so I'm just trying to understand it.

Thanks!!

Mike

My question is similar so I'll ask it here: do the spaces before and after the "=" make a difference? They always seemed to be used, but is that a matter of convention or is it necessary? Also, are other similar instances of spacing necessary?

4 Answers

Michael,

The plus sign concatenates strings--it's the "glue" sticking them together. If you don't need to do that, you can use the comma. Why use the "+" then, you ask? Because you might not want a space. For example, when concatenating parts of a URL.

Michael Walker
Michael Walker
12,466 Points

Ah, thanks for clarifying that James.

I'm really not great with javascript, but just an observation:

If you add an alert to your code like this:

var name = "Michael"; console.log("Hello", name)
alert("Hello", name)

You'll see that with a comma, it looks fine in the console but the name does not show in the alert. Change the comma to a + and the name shows.

var name = "Michael"; console.log("Hello", name)
alert("Hello " + name)

Again, I'm not great with javascript so take everything I've said with a pinch of salt!

Michael Walker
Michael Walker
12,466 Points

I see your point James. The plus sign is more of a catch all where the comma only works in certain instances. Point well taken sir!

Prathom Satapronpinyo
Prathom Satapronpinyo
12,759 Points

I'm really not an expert in Javascript. just on the learning track

just to share what I've learnt.

I think, the comma operator is use to evaluates both of its operands (from left to right) and returns the value of the second operand.

for example

var name = "Michael"; var a = ("Hello", name); console.log(a);

Michael

as u can see it'd return the second operand.

var name = "Michael"; var a = ("Hello " + name); console.log(a);

Hello Michael

it would return Hello Michael

when concatenating strings together the "+" sign is the right way to do I hope, this might help

*I've found out some info that I'd like to share with u this might answer your questions

console.log is act like a function what it does u can include as many arguments as u want by inserting "," (comma) between each argument and they will be replaced by spaces.