I have completed this challenge, But i don’t quite understand return.
First of all,
What is return?
Where should i use return? And what happens if i don’t use return?
In this challenge it says “Use a return statement to send a value back out of a function”. Out where of the function?
Can you please explain this to me? Am i overcomplicating things? , Because i had opened posts on the forum for javascript for some very simple lessons that seemed silly after i cracked it .
Your code so far
function plusThree(num) {
return num + 3;
}
const answer = plusThree(5);
Challenge Information:
Basic JavaScript - Return a Value from a Function with Return
For the first two questions the best will be seeing it with your own eyes.
console.log(answer);
Add the code above below the line with const answer. In console will be displayed the value of answer - which is what is returned by plusThree function. Then remove the return and see what changes.
Done…
With return it showed 8 on the console. And without return it showed undefined.
So does it mean that, if the code inside a function doesn’t have a return, the code can’t be calculated outside of the function. If that’s so, does it explain my 3rd doubt?
Thank you
return allows to return something (some explicit result) from function.
If function doesn’t return something explicitly, it will return implicitly undefined.
Out of a function means just that something is returned from function.
It doesn’t mean though that whatever happens in function is completely isolated from the outside of function. To some extent function can impact the other pieces outside even if function doesn’t return anything. Usually when that happens it’s called a side-effect, as it can be hard to keep track of what is changing what and when. More on this will be introduced in further challenges.
Why is it so hard to understand? I’m extremely sorry that I’m annoying you with this topic. I runned the same code in the browser console now, and its throwing an error (sometimes). How did you learn about return?
Does return in javascript have any other meaning than the word return literally?
Its been an hour trying to understand this topic but now i seem to make some sense.
I understand what you said now.
The return transfers num + 3 literally. And without return, const answer has no idea what value to give to plusThree. Am i right?
It’s fine, it can be complicated. You might wonder why there isn’t a short and easy answer for your questions. Reason for it is they ask for a kind of edge cases. The most accurate answer for them would be: it depends, followed by explanations including concepts that are introduced further along the way and would make this harder to understand.
On the other hand, the most literal answers would be:
return allows to return something from the function.
It can be used in function, if function should return something. If function will not have explicit return, then it will implicitly return undefined.
From the function to the point at which function is called.
Which aren’t too clear either . Keep in mind, with time and more practice all of this will start falling into the right places.
I think it’s pretty close to the literal return word, but I’m not a native speaker.
Ohh now i Understand…things are getting a bit clear…I think my browser console confused me…it threw me an error for the exact code which i did right.
As you said,
I understand things in a way that does not make sense.
I’ve read in the forum that javascript is tougher than HTML/CSS; with this in mind I always overcomplicate things by believing its tough. Silly me
Thanks a lot for having patience and clearing my doubt