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

Sufiyaan Haroon
seal-mask
.a{fill-rule:evenodd;}techdegree
Sufiyaan Haroon
Full Stack JavaScript Techdegree Student 9,558 Points

What does it mean???

'What does this code mean:'

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

especially this line:

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

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>

2 Answers

Tomasz Halaczkiewicz
Tomasz Halaczkiewicz
21,845 Points

It means you want only a Year from the entire date. For example: today is Saturday Jul 15 2017 [Day of the week, Month, Day, Year] and when you say to a program .getFullYear() it gives you only '2017' and doesn't give you month nor day.

I hope that's clear enough, don't hesitate to ask again though.

Tomasz Halaczkiewicz
Tomasz Halaczkiewicz
21,845 Points

The Date object is used to work with dates and times.

Date objects are created with new Date().

The Date object is build-in to JavaScript so you can just use it. In this case you use it to get the current year, in fact you are getting full date in the moment you call it and this part ".getFullYear()" indicates you only want the year part of todays date