Please someone help me with return


#1


Hi guys. So I haven't been having any trouble with my JS, it's just that I have no idea what the return statement does. It's been in the back of my head for weeks and I just can't come to a simple and clear conclusion as to what it does. I already know it stops the execution of a function, but what else? Please make your answer clear as I am a coding beginner. Also please don't link me to other sites. I can bet confidently that I checked all of the sites that you might think to recommend. Thanks guys. :smile:


Replace this line with your code.


#2

The return statement exits a function and returns a value to where the function was called from.

Are you confused about the idea of returning a value?


#3

Good Evening @awesomestcodereva, you're right the return statement does in some degree stop execution of a function. It would be more accurate to say that it ends or exits the function it resides in. Yet that isn't all.

What return really does, and should be used for is at a point in a function that you want a value "returned" or gotten which would make the function/script continuing pointless.

Lazily Logging Value

Let's first contrast what we're about to learn with the simpler methods that you might learn in an introductory example; not at all what you will end up using in more advanced JavaScript. We simply want to do some practice with dynamic functions.

function myName(string) {
  console.log(string);
};

myName('Michael'); // Will log 'Michael' to the Console

But can we do anything else with this function? Let's try to build a list of student names.

var students = [];

students[0] - myName('Michael');
students; // Will return an empty array

Why is this? Well the function merely logs the parameter to the console which does not reside between our array and the function, yet another area entirely.

Actually Returning Values

Now consider this example; Here is a simple example using a sum function. We want to get the result of the calculation from the function however & wherever we use it.

function returnSum(a, b) {
  return a + b;
}
returnSum(1,2);
3 // Returns a value of three when written in console.

And here is an example of how the returned value interacts with a container.

var sumValue = returnSum(1,2);  // Returns the result into the variable
sumValue; // Equal to 3

You can see now how return gets us the value of a function right where we need it. You can even combine our first example of logging to the console.

console.log(returnSum(1,2)); // Logs 3 to the console

Here is a more complicated example using an object. We have an object with some parameters, yet we want to self-reference another parameter, known as a "getter" method (because it gets a value). This is how we can use a return value to get a dynamic value.

var classroom = {
  students: ['Michael', 'awesomestcodereva', 'codecademy'],
  teacher() { return this.students[2]; }
}

classroom.teacher(); // Returns 'codecademy'.

So here are some examples and ideas of how you would use a return statement and where it is useful.


#4

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