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 trialOmar Farag
4,573 PointsHow 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
8,146 PointsHe 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
4,573 PointsThanks man
Rick Buffington
8,146 PointsNot a problem.
Omar Farag
4,573 PointsOmar Farag
4,573 PointsOh, so you can't return a string and a boolean?
Rick Buffington
8,146 PointsRick Buffington
8,146 PointsCorrect, 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: