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

Returning a Value from a Function

Inside the function's code block add this line of code var year = new Date().getFullYear(); This creates a new variable and stores the current year in it. Now, add a statement that returns this variable from the function.

it keeps on getting me error when i put the code in what do i do?

script.js
function getYear () {
}
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>
Abe Layee
Abe Layee
8,378 Points

All you have to do is use the return statement below the variable once you defined it. The return statement ends function execution and specifies a value to be returned to the function caller. Check this out for more details https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

function getYear() {
  var  year= new Date().getFullYear();
return year;
}
geoffrey
geoffrey
28,736 Points

Abe Layee You should move your answer as answer and not as comment, as this one is the correct answer It could be "marked as best answer".