Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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');