JavaScript Tutorial for Beginners – 14 – Return statement




In this video we examine the return statement.

Original source


13 responses to “JavaScript Tutorial for Beginners – 14 – Return statement”

  1. I dont get why return is needed in your example function? The value is already stored in the result variable. You can just alert(result) to see that the value was stored in result. Lines 4 & 8 are not needed.

  2. great tutorial mr.Ej, though I need more brain power now I think, to remember all what you taught…. now I can feel the real challenge of being programmer….. java will become my best friend.

  3. I greatly appreciate your tutorials. I'm having a hard time with return statements myself. I'm using multiple resources to try and get a good understanding, but it's not easy if this is your first programming language.

    For one thing that is hard to understand, in the example at 1:33, it looks like this is supposed to be adding the two numbers X and Y together… but isn't * the multiplication sign?

  4. +EJ Media First and foremost, I wish to thank you heartily for the series of your videos, which are always extremely clear and detailed. I am a complete beginner in programming. Nevertheless I was able to study and understand your previous tutorials about Html and CSS without problems. A few weeks ago I started off watching your series on JavaScript and the things began getting more difficult. I should point out that this happened not because I found the quality of your explanations worse, less clear. Simply you introduced concepts like loops, conditionals, functions, variables and so forth, I had never heard of before. Surfing the Web, talking to my acquaintances and friends I was told that for a complete beginner such an hurdle I found is absolutely normal, since JavaScript is more abstract than Html and CSS. So lots and lots of patience, motivation and practice will be necessary on my part. Having said that, however, in all honesty I think to have understood your first 13 tutorials on JS without coming across particular troubles. On the contrary, what I can't understand at all is the content of this tutorial about the "return statement" and so I politely ask for your help. I watched and watched this video and I even transcribed almost all your explanation.
    Nothing to do. I cannot get out of this problem. Now, I'm gonna proceed step by step. When transcribing your explanation, I split it up into four parts:

    PART 1)
    function add (x,y) {

    result = x * y;
    document.write(result);

    }

    add(6,5);

    /*
    We're going to continue along with JavaScript syntax and we've been talking a lot about
    functions and so far we've just been passing information into our functions. So this 6
    would go into the x and the 5 would go into y [add(6,5)> add(x,y)]. Then they would be
    multiplied and in this case the function would write out the result(30), which of course is
    our variable (result), and if we ran this, this would be written out to the browser.
    The key point here is: the function is doing this [document.write(result)], and the
    other point is that we're only sending information into the function [add(x,y)].
    But, what if we wanna send information back to where we call the function from [add(6,5)]?
    That is done with the "return statement". So, again, we can send information into a
    function [add(x,y)], and we can also have the function [add(x,y)] return information
    [add(6,5)] and that's what we're gonna cover in this video. So we use the return
    statement.

    */

    In particular, I cannot understand when you say "what if we wanna send information back to where we call the function from [add(6,5)]? That is done with the "return statement". So, again, we can send information into a
    function [add(x,y)], and we can also have the function [add(x,y)] return information
    [add(6,5)]"
    WHAT DO YOU YOU MEAN EXACTLY WHEN SAYING "SEND INFORMATION BACK"? AND WHAT IS THE UTILITY OF SENDING INFORMATION BACK? AGAIN, MOST OF ALL, I AM TOTALLY UNCLEAR ABOUT HOW "THE RETURN STATEMENT" ACTS AND ALLOWS TO DO THAT, HOW SUCH A PROCESS WORKS. I perfectly appreciate that in writing it's very hard to get across the exact nature of problem I find in understanding and that a face to face conversation would probably be necessary. Anyway I am gonna point out the statements in the remaining part of your class which sound to me particularly weird and obscure. So, I will go ahead.

    PART 2)
    function add (x,y) {

    result = x * y;
    return result;

    }

    add(6,5);

    /*
    So let's go ahead and get rid of this document.write (result) because we're not
    going to have the function do this now, we're actually going to use a return
    statement here. So, copy and paste that in here, and so now what will happen is
    the value that we get after we multiply this [x * y] will be returned to whatever
    called the function and in this case of course we called it right down here
    [add(6,5)] when we specified that add function. Now we could just return
    it to here [add(6,5)] but let's actually return this to a variable. So let's make
    a variable here and I should also point out that you don't have to necessarily just
    specify the function name [add] to call the function: variables can call functions
    also, and we've actually done that in earlier videos. So, again, you don't have
    to just specify the function name. You can actually create a variable and a variable
    can call in the function. So, I'll show you that now.
    */

    Sentences particularly obscure: "what will happen is
    the value that we get after we multiply this [x * y] will be returned to whatever
    called the function and in this case of course we called it right down here
    [add(6,5)] when we specified that add function"

    PART 3)
    function add (x,y) {

    result = x * y;
    return result;

    }

    var theResult = add(6,5);

    /*
    So let's type in the variable keyword. We'll call this variable theResult and of course
    will have to use the equals operator and there you can see now we've got a variable
    and its kinda doing the same thing right, we're actually gonna call it but we're doing
    it with a variable and so let's go ahead and save this, and let's go ahead and run this.
    We didn't get anything and I expected that and I'll explain why. The reason is
    we didn't do a document.write, but before I get to that let's actually walk through what
    would happen. So, again the variable (theResult) called the function and we did the same
    thing as when we just used the function name before [add(6,5)]. We passed in a 6 and a 5, it
    came into our parenthesis, and then it was multiplied right here and here [return result;]
    it works a little bit different again. We returned the result back to the variable
    (theResult), so we sent something back, we sent the value back to theResult variable, that
    called the function in the first place [function add (x,y) {…}]. So the value of this
    (theResult) now is actually 30. Now the reason we didn't see that is because we needed do
    something with this variable now, right? We didn't do anything with this variable.
    */

    Sentences particularly obscure: " again the variable (theResult) called the function and we did the same
    thing as when we just used the function name before [add(6,5)]. We passed in a 6 and a 5, it
    came into our parenthesis, and then it was multiplied right here and here [return result;]
    it works a little bit different again. We returned the result back to the variable
    (theResult), so we sent something back, we sent the value back to theResult variable"

    PART 4)
    function add (x,y) {

    result = x * y;
    return result;

    }

    var theResult = add(6,5);
    document.write(theResult);

    /*
    So let's actually go ahead and write this out and so if we go ahead and do a
    document.write and we write out the variable, let's go ahead and save this
    and now we should get the result of 30. There you see, we got the expected result.
    So, again, all you're doing when you use the return keyword is you're sending
    something back to whatever [in this case, var theResult = add(6,5);] called the
    function in the first place [function add (x,y) {…}].
    Okay, that's going to do it for this video. I will see you guys in the next video.
    */

    Sentences particularly obscure: "So, again, all you're doing when you use the return keyword is you're sending
    something back to whatever [in this case, var theResult = add(6,5);] called the
    function in the first place [function add (x,y) {…}]. "

    I do apologize for the overall length of my "particular question" (indeed, question is not the most appropriate noun :-)). However I'd really like understanding this tutorial as I believe that the concepts you have explained herein are highly important before going on. So I kindly ask you to help me understand somehow.
    I thank you really very much and I look forward to hearing from you at your earliest convenience.

    Robert

Leave a Reply