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 Passing an Argument to a Function

Variable Not Being Stored from Called Function

I have tried about every which way I can think of for this code to worked and reach watched videos from this unit and to me it seems as though this code that I have written should work. The first part of the task I complete no problem. I create the function, and the return to be able to return whatever value is called. The second part of the task I keep getting the error stating that I am "not storing the value in the variable", but the variable is set for the value of my parameter "anything". Not sure what I am missing.

Please review and advise.

script.js
function returnValue (anything) {
    return anything;
    var echo = anything;
}
returnValue ('hello');
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

Hey David, So there's an important thing to understand about functions and the "return" statement, which is that as soon as a return statement is reached, the function immediately stops processing all other code inside the function. What you have there, you're calling the function and passing the string "hello" into it. The function immediately sees a return statement, so it immediately returns the string you passed in. The var echo = anything line is never read or evaluated. Try moving that line above the return statement, and then I'm willing to bet that you should return echo, which will still be the string you passed in since that's the value it's been assigned, but that would give echo some reason for being there.

Hope that helps you, let me know!

Eric,

You are a lifesaver!! I would've been willing to bet money that I had tried that at some point, but clearly I had not. I guess I interpreted the instructions "After your newly created returnValue function" literally and just assumed it needed to go after.

Thank you!

Awesome! Glad I could help.