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

Jimel Atkins
Jimel Atkins
2,093 Points

add a statement that returns this variable from the function.

Im not sure what to write here...

I wrote return 'year';

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>

1 Answer

Hi Jimel,

Try return year; without the quotes.

When you put quotes around it then it's a string. Without the quotes it's treated like a variable.

Jimel Atkins
Jimel Atkins
2,093 Points

Thank you!

Is there a reason why in the examples in Javascript with functions he is using quotes around what he wants the return value to be? I noticed this thoroughly which is why I used quotes

If you want to point out a specific example from the videos then I can take a look. You can link to the video and mention the approximate time for the example.

Without seeing the code I would guess the intention was to return a string.

Sometimes you want to return strings but in this case it was a variable.

Jimel Atkins
Jimel Atkins
2,093 Points

In Getting Information From A Function at the 1:00 min mark is what Im referring to. Im assuming that this is different than in Introduction with Functions where he wrote an alert with the message in quotes. Im assuming here when you are trying to have the function return a phrase to the user you would use a string as a string can be written to the browser for display.. Am I correct if Im understanding that correctly?

At 1:07 I see

return "Espresso is on its way.";

In this case, Dave wanted the function to return a message in the form of a string. So quotes were required.

This type of a string is called a string literal.

at around 2:30 Dave changes the random number function to return the random number instead.

function getRandomNumber() {
    var randomNumber = Math.floor( Math.random() * 6 ) + 1;
    return randomNumber;
}

This is actually very similar to what this code challenge is asking you to do. A result is assigned to a variable and then you return that variable. Dave didn't put quotes around randomNumber so that it could be interpreted as a variable.

If you put quotes around it, then it's like the earlier "Espresso" example. You'd only be returning the literal string "randomNumber"

As far as the alert and message with quotes, Dave was probably passing a string to the alert function so that it could be displayed in an alert box.