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

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?