JavaScript JavaScript Basics Creating Reusable Code with Functions Returning a Value from a Function

Courtney Meza
Courtney Meza
2,403 Points

Can't return the variable from the function

I've tried numerous different ways, including console.log(); at the end and alert(); I've tried new Date() as well as newDate() in case it was an error. What am I missing?

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

Steven Parker
Steven Parker
173,890 Points

The created variable is named "year", but the return statement is trying to pass back the result of calling "getFullYear()" as a function instead.

To return the variable:

    return year;