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 trialJonathan Martinez
Full Stack JavaScript Techdegree Student 6,439 PointsI'm confused about the return statement.
In this particular video, Dave returns two arguments and an empty string that are concatenated. I get that, but what I don't understand is how he says only one value can be returned by a return statement. If you concatenate different data types, does that mean it is considered one statement? Also it is to my understanding that the return statements exits a function. Out of curiosity, what happens if you have two functions in which the first one did not have a return statement, and the second one does? Is this even possible?
3 Answers
Chyno Deluxe
16,936 PointsThe return keyword allows you to send data back from a particular function. For example.
function addNum(num1, num2){
return num1 + num2;
}
addNum(5, 10); // returns 15
The return keyword lets the function know that when the function is call, it must send back data. However, notice the difference when you omit the return keyword.
function addNum(num1, num2){
num1 + num2;
}
addNum(5, 10); // undefined
Not all functions require the return keyword. Only in cases where you need/want to test certain conditions and only return the response of those conditions.
I hope this helps. Feel free to ask me if you still have questions
jag
18,266 PointsIf you concatenate different data types, does that mean it is considered one statement?
If you concat different values into one when it's returned it will be one value.
Also it is to my understanding that the return statements exits a function.
Yes, which is why multiple returns can't be done.
Out of curiosity, what happens if you have two functions in which the first one did not have a return statement, and the second one does?
Is this even possible?
Without a return statement you just won't get a value. These functions exist to do other stuff but the way in the video it's being used is to concat and return the value.
Both functions are done.
Here is a codepen example.
Ran ShemTov
14,148 PointsYou'd use a function with no return value to manipulate data.
lets say i have a div with the ID of "bar" i want to dinamiclly set html in. i'd do:
function foo(html){
document.getElementById("bar").innerHTML=html;
}
no return. it will manipulate the dom and end the function. I do not need any value back