Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Calling a function and storing the returned variable.

In Javascript basics, I'm being asked to call the getYear function and store the returned value in a variable called yearToday. I've tried several things and it just isn't working. I'm hoping my code is attached to this post.

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

Hey this is what I got for ya; I want to say you don't need the semicolon when you return a value..semicolons in JS are a huge headache because sometimes you randomly don't need them and also you just forgot to store the var... so close!!! Good luck :)

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

};

Steven Parker
Steven Parker
215,984 Points

Actually, ending each statement with a semicolon is considered a "best practice", whether they are needed or not.

5 Answers

Hi Anthony

You're nearly there, just need to store the return value in a variable called "yearToday":

function getYear() {
  var year = new Date().getFullYear();
  return year;
}
var yearToday = getYear();
Steven Parker
Steven Parker
215,984 Points

You've got the "calling" part right.

When you call a function, you put parentheses after the name. So you got that part.

So when you store something in a variable, you perform an assignment operation. The assignment symbol is equal sign ("="). What you are assigning (the function call) goes on the right side. Where you are putting it (the variable) goes on the left side. Then all you need is the keyword "var" in front of the variable name to show that it is being created.

I'll bet you can get it now without an explicit spoiler.

Kerry Smyth
Kerry Smyth
3,921 Points

Not the answer to the question but I always loved this snippet for website footers.

<script>document.write(new Date().getFullYear())</script>
Steven Parker
Steven Parker
215,984 Points

To avoid confusion, when contributing something that is not an answer, you can create it as a comment instead.

Really appreciate it guys, I think I tried everything except for what Chris posted.