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

i don't understand the point of return

i am super confused by this return and am not sure what to put as the variable after return i tried writing getYear

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>

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing fine but the return is supposed to go inside the function. Some piece of code is going to call that function and ask for the year. And we want to send back the year to the piece of code that called it.

Here's sort of a practical example. Let's say that you have an accountant who is brilliant with money/finance. You turn in all your receipts and financial data to said accountant and they run the numbers. But if they never return the results of their calculations, it didn't do much good, did it? This is sort of the same.

We're going to call that function that no matter how many times we call it will always get the year. Then it will send us back the year so the piece of code that called it knows.

Hope this helps, but let me know if you're still stuck! :sparkles:

ok yea i guess i just don't understand why i don't put getYear but put year

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Ah ok. Because you're not calling the function. You're defining the function. The function is named getYear. So when you call it later, you will call it it by saying something like var currentYear = getYear();. When that happens the function getYear() will run. But you declared a variable inside the function definition named year which creates a date and gets the full year. So now year is equal to today's full year. But we have to return the result of that calculation to the call. The value that is stored in the year variable inside the function will now be assigned to currentYear elsewhere in our code.

Hope that clarifies things! :sparkles:

this concept just isn't making sense to me gonna look for some other videos i think im just not getting it