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

Anna Parker
PLUS
Anna Parker
Courses Plus Student 4,399 Points

function return

I'm confused by this challenge, particularly this piece of code:

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

Specifically, I don't understand the var with two functions, or what the word 'new' means. Why isn't it connected to 'Date'?

Thanks!

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

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

I think you may need to re-watch a few videos to better understand objects and functions but I will walk you through understanding this so here we go:

So what is this new thing? So new basically creates a..well...new instance of an object that can be used or attached to a variable.

function Dog(name, species, noise) {
     this.name = name;
     this.species = species;
     this.bark = function() {alert(noise)}
}

So if we look at the function above we see that we have a Dog function, that has the properties: name, species, and bark. Both name and species are relatively simple variable, but bark is a function. If we wanted to create a dog, we would need to assign a variable to that function. But there has to be a better way right? What if I want to have multiple dogs? Well that is where new comes in. New allows us to create a new instance of this dog function and assign it to something (or just access its properties. So let's do just that:

var lucky = new Dog("lucky", "dalmation", "woof");
var buster = new Dog("buster", "yorkie", "yip");

Now let's say that we just want to get the bark of a dog, we can do it like this:

var bark = new Dog("lucky", "dalmation", "woof").bark();

Calling bark would cause an alert with text "woof".

Are you getting it now?

Anna Parker
PLUS
Anna Parker
Courses Plus Student 4,399 Points

I rewatched the video, and I get your example (thanks!), but I don't understand how to return the variable from the function in the challenge question.

Dane Parchment
Dane Parchment
Treehouse Moderator 11,075 Points

Ok, so basically you are creating a function called getYear():

function getYear() {}

Then we need to create a variable that retrieves the getFullYear of the Date object and return it:

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

Effectively this creates a variable that holds the value of getFullYear() and then makes the function return that value, so that whenever we call getYear() it returns the current value of the year variable. So the next step is calling the function to get said value:

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

var yearToday = getYear();

And that's it you are done!