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

R R
R R
1,570 Points

Passing in a literal string value to a function

The challenge question asks to pass in a literal string value - giving the sample "My argument" - and store the results in a variable called echo. I passed the challenge but realized i didn't use quote marks when I passed in the argument. I know using quote marks means the value is a string. But what is the difference between this code:

function returnValue(singleArgument) { return singleArgument; }

var echo = returnValue("My Argument");

and this code:

function returnValue("singleArgument") { return singleArgument; }

var echo = returnValue("My Argument");

Could I have written the return statement like this?

function returnValue("singleArgument") { return "singleArgument"; }

var echo = returnValue("My Argument");

script.js
function returnValue(singleArgument) {
  return singleArgument;
}

var echo = returnValue("My Argument");
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

William Li
PLUS
William Li
Courses Plus Student 26,868 Points
function returnValue("singleArgument") { return "singleArgument"; }

Could I have written the return statement like this?

No, doing that will get you a syntax error. When defining a function in JavaScript, it accepts only parameters, 0 or more. And String literal is not a valid name of function parameter; Only when calling the function, you can pass a string literal to it as argument.

R R
R R
1,570 Points

Thanks, William