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 JavaScript Basics (Retired) Creating Reusable Code with Functions Returning a Value from a Function

Sean Lafferty
Sean Lafferty
3,029 Points

Don't know how to do this :(

HELP!

script.js
function getYear() {
var year = new Date().getFullYear();
  return year();

}
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
Kevin Egstorf
Kevin Egstorf
26,590 Points

Hi Sean,

First you do not call the function, so it will never run and second you can not return year() like that, it is not a function but a variable so it would look like this return year; . plus it would be better in using const or let instead of var

I hope this is helpfull if you have anymore questions just let me know.

if you want to output the year in the console it would look like this.

function getYear() {
var year = new Date().getFullYear();
  return year;
}

console.log(getYear());

1 Answer

You are asked to return the variable 'year'; the code you have written 'return year()' is treating year as if it were a function (the parentheses after an identifier are only used to call a function). You just need to remove the parentheses and write 'return year'.

In summary:

year == a variable identifier year() == a function call You will usually be returning variables, not function calls.