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

Robert Parson
Robert Parson
6,365 Points

Why does rgb(); work inside backticks and not outside of them?

Why does rgb(); work inside backticks and not outside of them?

2 Answers

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hi Robert Parson 👋

It's a bit hard to tell without having more context to it. Could you please share the video you're working on and maybe the code snippet you're talking about? 🙂

Without the context I can only assume that you're trying to use the rgb() function in CSS and you're dynamically updating it using JavaScript. When doing this you're altering an elements CSS properties for example:

element.styles.color = "rgb( 0, 0, 0)";

In this example the color value is expecting a string, without the quotes you'd try to call a JavaScript function called rgb which most likely doesn't exist in your code 🙂

I hope this clears things up! If not please share a link to the video and/or your code and I'd be more than happy to elaborate 😃

Steven Parker
Steven Parker
231,269 Points

The backticks create a string which can then be included as part of the "style" attribute in the HTML code. The browser will then interpret that and establish the color.

But outside of the backticks, "rgb()" would be interpreted as an attempt to call a JavaScript function by that name, and since no function of that name has been defined it would cause an error.