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

David Loder
David Loder
3,480 Points

How to get this part of code to return the function into a variable?

So far I can't get the var to be assigned to the correct function call return code

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

}
var yearToday = return 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>
eslam said
eslam said
Pro Student 6,733 Points

When you have a function and you want it to return a value it should be inside the function itself like so:

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

you can test it by logging it to the console like so

console.log(yearToday);