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

Dwayne Munro
seal-mask
.a{fill-rule:evenodd;}techdegree
Dwayne Munro
Front End Web Development Techdegree Student 13,108 Points

how to do returning a value from a function

how to solve task 3

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

Hey Dwayne Munro, In 3rd task they only asked you to call function and assign return value to var yearToday

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

2 Answers

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

Hi there! You're doing great and you're on the right track. You set up the yearToday variable and then on a new line you call the getYear function. The getYear function is going to look up the current year and then send it back to the piece of code that called it. But you never do anything with the returned value. Take a look:

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

This last line does two things. It sets up our variable yearToday. Then it calls the getYear function. The getYear function will deterimine the year and send it back. That year now gets assigned as the value of our yearToday. Hope this helps! :smiley: