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

Tobias Jackson
Tobias Jackson
9,758 Points

return what ?

This creates a new variable and stores the current year in it. Now, add a statement that returns this variable from the function.

script.js
function getYear(){
var year = new Date().getFullYear();
}
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>

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

4 Answers

William Barnes
seal-mask
.a{fill-rule:evenodd;}techdegree
William Barnes
Front End Web Development Techdegree Student 6,597 Points

A return statement just returns a certain variable from the function, so if you add a " return year; " to the function it will return the value of year.

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

So then if you were to create a new var called currentYear and call your getYear() function it would return the value "year".

var currentYear = getYear();

you need to return the value of the variable.

This line, creates a new variable (called year) and stores the current year in it:

var year = new Date().getFullYear();

We want to return the variable (year) from the function.

Hope this helps :)

Carlos José
PLUS
Carlos José
Courses Plus Student 12,431 Points

You can actually just use the Date class.

var actualDate = new Date();

console.log(actualDate);

console.log(actualDate.getFullYear());

As requested in the challenge

function getYear(){

var year = new Date().getFullYear();

return year;

}

You can assign a Returned value to variables like this : var assign = Function*(arguments, arguments)*;