JavaScript JavaScript Basics Creating Reusable Code with Functions Returning a Value from a Function

Alex Lang
Alex Lang
1,110 Points

Help me understand this a bit better?

Okay so in this code why can't you call it using the ().

for example like this: return year();

instead of

return year;

3 Answers

Zach Swift
Zach Swift
17,972 Points

When you create a new date and call the getFullYear method, it returns a number (like 2018). That is what you're storing in your year variable. The open and close parentheses are for calling functions. Since the year variable is just holding a number, there is no reason to call it like a function.

Steven Parker
Steven Parker
173,644 Points

The name "year" in this challenge is a scalar variable, not a function. You are correct that parentheses are added to a function name to call it, but that does not apply to variables that represent values. Those variables are not called, they represent the value assigned to them by their name alone.

Zach Swift
Zach Swift
17,972 Points

Steven Parker, it's possible and likely that a variable could be representing a function. In that case, you could call it with parenthesis. But in this case the variable was a number, so no reason to call it.

Steven Parker
Steven Parker
173,644 Points

More than just "no reason", it's not valid syntax to call a variable that represents a value. But you're right that variables can refer to functions. I revised my wording a bit to emphasize the distinction.

Zach Swift
Zach Swift
17,972 Points

You're 100% correct on that.