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 trialAnudeep Nayakoti
2,032 PointsI think return statements do return multiple values :
function sayHello() { var racha='hello'; return 6 + " " +racha; } sayHello();
For the above following code, it did return output as '6 racha' on console;
Anudeep Nayakoti
2,032 PointsIt did return two values right? one is 6 and the other is the value of variable that is stored inside the variable name 'racha'; Bottom line: Multiple Values;
2 Answers
Steven Parker
231,269 PointsYour statement creates one value and then returns it.
The expression 6 + " " + racha
is a string concatenation. It causes the "6" and the space and the value stored in racha to be combined into one string. The final value is a string containing "6 hello", and it is this single value that gets returned.
So return statements only return one value, but that one value might be made from several other values.
Anudeep Nayakoti
2,032 PointsNot satisfied. Sorry because it does combine/compress two different values into one as string and returning it; Obvious some values if it's more than one, it is multiple right ? Reason:racha is just one variable but it could be many different variables above racha and have many different values. All will be returned but by converting everything into one whole string;
This following code will return three different values: function sayHello() { var racha='hello'; var text="wow"; alert('function ran'); return 6 + racha + text; } var data1= sayHello();
Steven Parker
231,269 PointsNo, it still returns only one value. Your expression makes a single new string using 3 others. But the return statement is returning just the single value that was made from the 3 parts.
The rule about returning one value doesn't prevent you from making that value from several parts. And as pooya suggested, you can also return a value that actually contains other values, if the single value you return is an object or array. While this still observes the technical limitation of returning just one value, it gives you nearly the same power and flexibility as if you could return more.
pooya tolideh
12,184 PointsTo add to what Steven said,
JavaScript ALWAYS evaluates the expression on the right-hand side of the return statement first. (here, it's a string concatenation operation) Then, it passes the result of that procedure to return. So at the end, only one item is returned and that's a string.
IF you want to return multiple values, you can use an array or object. --> return [racha, 6]
pooya tolideh
12,184 Pointspooya tolideh
12,184 PointsNope. I literally copy-pasted your code in the console and it returned '6 hello'