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

CSS jQuery Basics (2014) Creating a Simple Drawing Application Perform: Part 2

Kenton Hubner
Kenton Hubner
6,314 Points

When implimenting the RBG colors, what is the purpose of adding a space before each variable r, b, g?

$('#newColor').css('background-color', "rgb(" + r + "," + g + "," + b + ")");

the above is what I am referencing. Without the spaces before or after the plus sign, the code does not work. What causes this and why?

2 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

Hi, Kenton. JavaScript doesn't care about spaces when it comes to concatenation, so this:

$('#newColor').css('background-color', "rgb("+r+","+g+","+b+")");

would work just fine as well. You may have accidentally erased a + or a " when doing so, which would cause issues and errors.

Jaime Rios
PLUS
Jaime Rios
Courses Plus Student 21,100 Points

As Ryan says, without spaces it should work fine as well. The main purpose of doing this is to make the code easier to read.