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 trialColin Grubbs
2,137 PointsWhy does calling "value" require value to have the 2 parenthisis after?
When he puts "value()" within the randomRGB function why does it have 2 parenthesis after it? Doesn't that mean that value is essentailly a function that can take a parameter?
Colin Grubbs
2,137 PointsSo essentially, randomValue does not have the () after it so when we use it as an argument, we have to include that when we access the value argument?kind of strange that it is included in the ${} since there is technically nothing named "value()". would you be able to do ${value}()???
John Johnson
11,790 PointsThat wouldn't work. Everything inside ${} in template literals (strings inside backticks) is executed like code. So ${randomValue()} inside backticks is technically equivalent to calling randomValue(). However, if you put ${value}() whatever you passed into value would be evaluated like a variable and the two parenthesis would be considered string. This statement will fail because you do not have a variable named randomValue.
For fun's sake, let's say you did have a variable named randomValue and you had stored the string "treehouse" ${value}() would evaluate to "treehouse()" as a string. This would not be executed like a function, since the parenthesis are being considered string.
1 Answer
Zaid Khan
12,769 PointsWhen he puts "value()" within the randomRGB function why does it have 2 parentheses after it?
Remember randomValue
is a function that is assigned as a value
parameter in randomRGB
.
Like this randomRGB(randomValue)
So if we are accessing the randomValue
function we need to give parentheses after value
.
John Johnson
11,790 PointsJohn Johnson
11,790 PointsValue itself isn't a function, but how it is being used in this instance is to call the randomValue() function from within the randomRGB function. Since randomRGB accepts one value, and we want that value to operate like a function, we add the parenthesis to make value a function call.
Basically, when you call randomRGB(randomValue), the following javascript will run