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 Giving Information to Functions

Omar Farag
Omar Farag
4,571 Points

How many values can the "return" keyword return?

In the video before this one, the instructor says that the "return" keyword can only return one value. However, in this video, he returns 3. Any explanations?

2 Answers

Rick Buffington
PLUS
Rick Buffington
Courses Plus Student 8,037 Points

He is correct, a function can only return one value/type. Where you might be getting hung up is when he is returning multiple variables that make up 1 single value. For example:

return "This is a string";      // This is 1 value
var name = "Dude";
return "Hi " + name;   // This is still 1 value (a string that returns "Hi Dude"), using a variable
var firstName = "Dude";
var lastName = "Awesome";
return "Hi " + firstName + " " + lastName;  // This is still 1 value (a string that returns "Hi Dude Awesome"), using 2 variables

So, as you can see you can return only 1 value, but that value can be made up from several variables of the same type. In this case multiple variables put together to return a String.

Omar Farag
Omar Farag
4,571 Points

Oh, so you can't return a string and a boolean?

Rick Buffington
Rick Buffington
Courses Plus Student 8,037 Points

Correct, not within the same return statement. However, you could return an Array or Object that contained a string and a boolean inside - which would accomplish that. Something like:

function getArray() {
    var myArray = [ "Wussup", true ];
    return myArray;
}