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

Prakhar Patwa
Prakhar Patwa
11,260 Points

i have a question regarding function

my js code is

function getArea(length , width)
    var area = length * width ;
    return "<h1>Area of <a href='#' style = 'color:red; text-decoration:none;' >Rectangle</a> is " + area + "</h1>";

document.write( getArea(34 , 22) );

============================================= how to take a user input in this? prompt("what is the length?"); prompt("what is the width?");

function returnValue(a){
  return a ;

var echo = returnValue('My argument');
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
<script src="script.js"></script>

2 Answers

You could either assign the return value of the prompt function calls to two variables as Vitor Freitas recommended, or you could just call the getArea function, passing in the prompt function calls as parameters:

function myFunc(x, y) {
  // function code here...

var x = prompt("What is x?");
var y = prompt("What is y?");

myFunc(x, y);


function myFunc(x, y) {
  // function code here...

myFunc(prompt("What is x?"), prompt("What is y?"));

Obviously the first is a bit easier to read and understand.

If you feel that your question has been answered, please select an answer as the 'Best Answer' so that other Treehouse users know that this question has been resolved.

Vitor Freitas
Vitor Freitas
3,579 Points

You can do it this way :

var length= prompt("what is the length?"),
    width = prompt("what is the width?");
Prakhar Patwa
Prakhar Patwa
11,260 Points

will it be in the function or in the argument?