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

Omar Farag
Omar Farag
4,573 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
Rick Buffington
8,146 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,573 Points

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

Rick Buffington
Rick Buffington
8,146 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;
}