# RGB variable syntax with "+R+", "+G+", "+B+"

**URL:** <https://forum.code.org/t/rgb-variable-syntax-with-r-g-b/6805>\
**Category:** Unit and Lesson Discussion\
**Tags:** csp-unit-5, csp-unit-5-lesson-10\
**Created:** [February 23, 2017, 1:59pm UTC](https://forum.code.org/t/rgb-variable-syntax-with-r-g-b/6805 "2017-02-23T13:59:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicholsonl](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@nicholsonl](https://forum.code.org/u/nicholsonl)\
**Post date:** [February 23, 2017, 1:59pm UTC](https://forum.code.org/t/rgb-variable-syntax-with-r-g-b/6805/1 "2017-02-23T13:59:55Z")

</div>

Hi,  
In 5.10 step 10-14 or so…Pardon my ignorance, but what is the rationale behind surrounding the R, G and B variables in + and " marks? I just need to be able to explain this to the students and I, frankly, don’t know the answer. Help!  
Lee

---

<div class="post-metadata">

**Author:** ![nicholsonl](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@nicholsonl](https://forum.code.org/u/nicholsonl)\
**Post date:** [February 23, 2017, 2:50pm UTC](https://forum.code.org/t/rgb-variable-syntax-with-r-g-b/6805/2 "2017-02-23T14:50:54Z")

</div>

Nevermind!!

var color = “rgb(”+R+","+G+","+B+")"; in the code is read as rgb( concatenated with the value of R concatenated with , concatenated with the value of G concatenated with , concatenated with the value of B concatenated with )

I didn’t start reading from the left to right and just looked at the “” on the outside of the whole expression first.

This makes perfect sense. Sorry to have bothered anyone with this. One of my sharper students politely clarified this for me.

---

<div class="post-metadata">

**Author:** ![mlarrubia](https://avatars.discourse-cdn.com/v4/letter/m/c5a1d2/32.png) [@mlarrubia](https://forum.code.org/u/mlarrubia)\
**Post date:** [March 3, 2017, 6:31pm UTC](https://forum.code.org/t/rgb-variable-syntax-with-r-g-b/6805/3 "2017-03-03T18:31:15Z")

</div>

Im a little confused just as you were. So does “++” mean concatenation in the javascript language?

---

<div class="post-metadata">

**Author:** ![terence.stone25](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/terence.stone25/32/5124_2.png) [@terence.stone25](https://forum.code.org/u/terence.stone25)\
**Post date:** [March 7, 2017, 9:38pm UTC](https://forum.code.org/t/rgb-variable-syntax-with-r-g-b/6805/4 "2017-03-07T21:38:11Z")

</div>

In JavaScript `++` is used to increment a value by 1 `( x = x + 1 )`. When concatenating Strings, you just need one plus sign: `var total = "The total is " + x ;`.

In this case we are combining random rgb values, using individual variables.  
To establish the random values, we first assign the variables to a range of numbers.  
`var r = randomNumbers(0,255);`  
`var g = randomNumbers(0,255);`  
`var b = randomNumbers(0,255);`

We then apply them to the rgb statement using concatenation and a short cut:  
`var color = rgb"( "+r+", "+g+", "+b+")";`

This is the full version of the code:  
`var color = "rgb("+randomNumber(0,230)+","+randomNumber(0,230)+","+randomNumber(0,230)+")";`

Since we declared and initialized variables for each rgb value, we use the simpler short cut. It reads as stated above:  
rgb( concatenated with the value of `r` concatenated with , concatenated with the value of `g` concatenated with , concatenated with the value of `b` concatenated with `rgb`);
