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

Shahar Ohayon
Shahar Ohayon
4,868 Points

Passing an Argument to a Function

I don't know what the F@#$@#$ I'm doing wrong! T_T

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

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points

The code inside the function is never executed, since you never call returnValue outside the function. You correctly call the function in line 3, but this code is not evaluated before you have called the function. Therefore the function call needs to go outside of the function. I will now assume that line 3 and 4 was written outside the function:

function returnValue(argument) {
  return argument;
}
returnValue('hello');
var echo = "returnValue('hello')";

The desired result to be stored in echo is the result of returnValue('hello'), but right now you are storing the string "returnValue('hello')" in echo instead, i.e, in the new line 5 you do not make a function call, but only writes that exact string value to echo. The solution would therefore be:

function returnValue(argument) {
  return argument;
}
var echo = returnValue('hello');

returnValue('hello') evaluates to 'hello' and 'hello' is then stored in echo.

Shahar Ohayon
Shahar Ohayon
4,868 Points

Thank you! I got so mad lol

Rafal Kita
Rafal Kita
5,496 Points

Yes it is confusing at the beginng. Took me some time to understend as well and that's the way I passed it.

function returnValue(abc) {
  return abc;
}
var echo = returnValue('alphabet');