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

What is a "new" operator? I also don't understand what I'm doing wrong. Please help

I don't understand the "new" operator and don't understand what I'm doing wrong in this task.

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

getYear ()

var yearToday = "year"
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>

I also don't get where the .getFullYear come from and what it is.

1 Answer

Erwan EL
Erwan EL
7,669 Points

new Date() and getFullYear() are two native functions of javascript you can try both of them in your browser console pushing ctrl + shift + i The new create an instance of Date() you can read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

new Date();
// return 
Sun Dec 10 2017 18:59:41 GMT-0500 (Amér. du Sud - Pac.)

new Date().getFullYear()
//return
2017

the getFullYear function filter the year of new Date()