Why use return?


#1

The instructions for this lesson said specifically to use retun and not console.log
I always get confused about when to use the two - why would I only be able to use the return keyword here?


#2

return is only used in functions. typically, the lessons will tell you when to use return, but you'll get used to it. I can't answer you really well, because i'm not sure so much about your question, but if you have more specific questions or want to know more about return, you can let me know. :slight_smile:


#3

https://www.codecademy.com/en/forum_questions/505b318b78e591000200e39d Ok so this is a very old discussion off of the old Codecademy forum page and I believe this guy named permalink does a good job explaining the difference. I will quote him below but I also wanted to give my source because I know bringing up old forum posts is not recommend especially from the old site. Anyhow here is his explanation:

"Hey luckynika,

There is actually a very clear distinction between these calls. console.log() is a function used to print information to the console. return on the other hand is a call to pass some value back up to where the call was made. For instance, let's say you create a function called square() that takes in a single numeric parameter and returns that value squared. Your function would look like the following:

var square = function square(x) {
return x*x;
};
You would call your function like this:
var ans = square(5);

This would set ans equal to the value returned by square() but will not print it to the console. Hence, the return statement is passing the value x*x back to the call of square. If you want to view the value in ans, you would write: console.log(ans); or to simplify the problem as a whole you could write: console.log(square(5)); and my guess is you have this last line in your code which is why you are confused.

Hope this helps,
Dustin =D"


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.