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

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: