Why does JavaScript ES6 function calls would not work as expected












1















The following snipped is given:



function output() {
return "<p>normal function</p>";
}

//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});


I was wondering, why a normal function would print correctly in the example above, but the second approach would just print the translated function declaration like that:



"function () {return "

arrow-function
";} "


Can you explain me that?










share|improve this question


















  • 5





    You are writing the method, and not the result of the method, like you do with output(). To make it work, you'll need to invoke the arrow function document.write((() => {return "<p>arrow-function</p>"})()) would work.

    – Ori Drori
    Nov 13 '18 at 22:14








  • 2





    Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich

    – Marc M
    Nov 13 '18 at 22:36











  • There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.

    – Marc M
    Nov 13 '18 at 22:46
















1















The following snipped is given:



function output() {
return "<p>normal function</p>";
}

//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});


I was wondering, why a normal function would print correctly in the example above, but the second approach would just print the translated function declaration like that:



"function () {return "

arrow-function
";} "


Can you explain me that?










share|improve this question


















  • 5





    You are writing the method, and not the result of the method, like you do with output(). To make it work, you'll need to invoke the arrow function document.write((() => {return "<p>arrow-function</p>"})()) would work.

    – Ori Drori
    Nov 13 '18 at 22:14








  • 2





    Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich

    – Marc M
    Nov 13 '18 at 22:36











  • There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.

    – Marc M
    Nov 13 '18 at 22:46














1












1








1








The following snipped is given:



function output() {
return "<p>normal function</p>";
}

//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});


I was wondering, why a normal function would print correctly in the example above, but the second approach would just print the translated function declaration like that:



"function () {return "

arrow-function
";} "


Can you explain me that?










share|improve this question














The following snipped is given:



function output() {
return "<p>normal function</p>";
}

//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});


I was wondering, why a normal function would print correctly in the example above, but the second approach would just print the translated function declaration like that:



"function () {return "

arrow-function
";} "


Can you explain me that?







javascript ecmascript-6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 22:12









Marc MMarc M

5281623




5281623








  • 5





    You are writing the method, and not the result of the method, like you do with output(). To make it work, you'll need to invoke the arrow function document.write((() => {return "<p>arrow-function</p>"})()) would work.

    – Ori Drori
    Nov 13 '18 at 22:14








  • 2





    Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich

    – Marc M
    Nov 13 '18 at 22:36











  • There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.

    – Marc M
    Nov 13 '18 at 22:46














  • 5





    You are writing the method, and not the result of the method, like you do with output(). To make it work, you'll need to invoke the arrow function document.write((() => {return "<p>arrow-function</p>"})()) would work.

    – Ori Drori
    Nov 13 '18 at 22:14








  • 2





    Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich

    – Marc M
    Nov 13 '18 at 22:36











  • There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.

    – Marc M
    Nov 13 '18 at 22:46








5




5





You are writing the method, and not the result of the method, like you do with output(). To make it work, you'll need to invoke the arrow function document.write((() => {return "<p>arrow-function</p>"})()) would work.

– Ori Drori
Nov 13 '18 at 22:14







You are writing the method, and not the result of the method, like you do with output(). To make it work, you'll need to invoke the arrow function document.write((() => {return "<p>arrow-function</p>"})()) would work.

– Ori Drori
Nov 13 '18 at 22:14






2




2





Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich

– Marc M
Nov 13 '18 at 22:36





Anyway: I've just came back here and see all the answers and comments now which must be happened within the last few minutes. So you really should calm down since I've not voted anything yet @jfadich

– Marc M
Nov 13 '18 at 22:36













There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.

– Marc M
Nov 13 '18 at 22:46





There are a lot of helpful and correct answers below - I had to select one so I chose the one which has the best quality in formatting and has the most extensive explanation.

– Marc M
Nov 13 '18 at 22:46












4 Answers
4






active

oldest

votes


















2














You aren't actually invoking the second function as you are with the first. If you wrap the function and invoke it you'll get the expected result.






function output() {
return "<p>normal function</p>";
}

//first version
document.write(output());
//second version
document.write(() => {return "<p>arrow-function</p>"});

//should be
document.write((() => {return "<p>arrow-function</p>"})());








share|improve this answer































    2














    It's because you are creating the function but not calling it.



    To call the anonymous arrow function you should do:



    (() => {return "<p>arrow-function</p>"})()





    share|improve this answer































      2














      You are telling document.write to output a function definition that you are passing and you aren't really calling that arrow function so it isn't returning a value. You could do this instead and get the results you are expecting



      var f = () => {return "<p>arrow-function</p>"};

      document.write(f())





      share|improve this answer































        2














        It is because the first parameter is a function call, whereas the second is only a function.



        For the second call to work as the first, you would do:



        document.write( (() => {return "<p>arrow-function</p>"})() );


        This syntax above is pretty messy: you need the extra () to call the function. On top of that, you need to surround all the arrow function definition within parentheses due to language syntax requirements.



        This way to immediately call functions from the very definition was really popular before ES2015 and is known as IIFE's. You usually see it with function statements but you can immediately call arrow functions too.



        On the other hand, for the first line of your snippet to work as your original second, you would do instead:



        document.write(output);


        So now you're passing just two function definitions to document.write.






        share|improve this answer

























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290287%2fwhy-does-javascript-es6-function-calls-would-not-work-as-expected%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You aren't actually invoking the second function as you are with the first. If you wrap the function and invoke it you'll get the expected result.






          function output() {
          return "<p>normal function</p>";
          }

          //first version
          document.write(output());
          //second version
          document.write(() => {return "<p>arrow-function</p>"});

          //should be
          document.write((() => {return "<p>arrow-function</p>"})());








          share|improve this answer




























            2














            You aren't actually invoking the second function as you are with the first. If you wrap the function and invoke it you'll get the expected result.






            function output() {
            return "<p>normal function</p>";
            }

            //first version
            document.write(output());
            //second version
            document.write(() => {return "<p>arrow-function</p>"});

            //should be
            document.write((() => {return "<p>arrow-function</p>"})());








            share|improve this answer


























              2












              2








              2







              You aren't actually invoking the second function as you are with the first. If you wrap the function and invoke it you'll get the expected result.






              function output() {
              return "<p>normal function</p>";
              }

              //first version
              document.write(output());
              //second version
              document.write(() => {return "<p>arrow-function</p>"});

              //should be
              document.write((() => {return "<p>arrow-function</p>"})());








              share|improve this answer













              You aren't actually invoking the second function as you are with the first. If you wrap the function and invoke it you'll get the expected result.






              function output() {
              return "<p>normal function</p>";
              }

              //first version
              document.write(output());
              //second version
              document.write(() => {return "<p>arrow-function</p>"});

              //should be
              document.write((() => {return "<p>arrow-function</p>"})());








              function output() {
              return "<p>normal function</p>";
              }

              //first version
              document.write(output());
              //second version
              document.write(() => {return "<p>arrow-function</p>"});

              //should be
              document.write((() => {return "<p>arrow-function</p>"})());





              function output() {
              return "<p>normal function</p>";
              }

              //first version
              document.write(output());
              //second version
              document.write(() => {return "<p>arrow-function</p>"});

              //should be
              document.write((() => {return "<p>arrow-function</p>"})());






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 13 '18 at 22:16









              Jordan SJordan S

              3,069921




              3,069921

























                  2














                  It's because you are creating the function but not calling it.



                  To call the anonymous arrow function you should do:



                  (() => {return "<p>arrow-function</p>"})()





                  share|improve this answer




























                    2














                    It's because you are creating the function but not calling it.



                    To call the anonymous arrow function you should do:



                    (() => {return "<p>arrow-function</p>"})()





                    share|improve this answer


























                      2












                      2








                      2







                      It's because you are creating the function but not calling it.



                      To call the anonymous arrow function you should do:



                      (() => {return "<p>arrow-function</p>"})()





                      share|improve this answer













                      It's because you are creating the function but not calling it.



                      To call the anonymous arrow function you should do:



                      (() => {return "<p>arrow-function</p>"})()






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 13 '18 at 22:15









                      Ariel AlvaradoAriel Alvarado

                      682311




                      682311























                          2














                          You are telling document.write to output a function definition that you are passing and you aren't really calling that arrow function so it isn't returning a value. You could do this instead and get the results you are expecting



                          var f = () => {return "<p>arrow-function</p>"};

                          document.write(f())





                          share|improve this answer




























                            2














                            You are telling document.write to output a function definition that you are passing and you aren't really calling that arrow function so it isn't returning a value. You could do this instead and get the results you are expecting



                            var f = () => {return "<p>arrow-function</p>"};

                            document.write(f())





                            share|improve this answer


























                              2












                              2








                              2







                              You are telling document.write to output a function definition that you are passing and you aren't really calling that arrow function so it isn't returning a value. You could do this instead and get the results you are expecting



                              var f = () => {return "<p>arrow-function</p>"};

                              document.write(f())





                              share|improve this answer













                              You are telling document.write to output a function definition that you are passing and you aren't really calling that arrow function so it isn't returning a value. You could do this instead and get the results you are expecting



                              var f = () => {return "<p>arrow-function</p>"};

                              document.write(f())






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 13 '18 at 22:16









                              Matti PriceMatti Price

                              2,012919




                              2,012919























                                  2














                                  It is because the first parameter is a function call, whereas the second is only a function.



                                  For the second call to work as the first, you would do:



                                  document.write( (() => {return "<p>arrow-function</p>"})() );


                                  This syntax above is pretty messy: you need the extra () to call the function. On top of that, you need to surround all the arrow function definition within parentheses due to language syntax requirements.



                                  This way to immediately call functions from the very definition was really popular before ES2015 and is known as IIFE's. You usually see it with function statements but you can immediately call arrow functions too.



                                  On the other hand, for the first line of your snippet to work as your original second, you would do instead:



                                  document.write(output);


                                  So now you're passing just two function definitions to document.write.






                                  share|improve this answer






























                                    2














                                    It is because the first parameter is a function call, whereas the second is only a function.



                                    For the second call to work as the first, you would do:



                                    document.write( (() => {return "<p>arrow-function</p>"})() );


                                    This syntax above is pretty messy: you need the extra () to call the function. On top of that, you need to surround all the arrow function definition within parentheses due to language syntax requirements.



                                    This way to immediately call functions from the very definition was really popular before ES2015 and is known as IIFE's. You usually see it with function statements but you can immediately call arrow functions too.



                                    On the other hand, for the first line of your snippet to work as your original second, you would do instead:



                                    document.write(output);


                                    So now you're passing just two function definitions to document.write.






                                    share|improve this answer




























                                      2












                                      2








                                      2







                                      It is because the first parameter is a function call, whereas the second is only a function.



                                      For the second call to work as the first, you would do:



                                      document.write( (() => {return "<p>arrow-function</p>"})() );


                                      This syntax above is pretty messy: you need the extra () to call the function. On top of that, you need to surround all the arrow function definition within parentheses due to language syntax requirements.



                                      This way to immediately call functions from the very definition was really popular before ES2015 and is known as IIFE's. You usually see it with function statements but you can immediately call arrow functions too.



                                      On the other hand, for the first line of your snippet to work as your original second, you would do instead:



                                      document.write(output);


                                      So now you're passing just two function definitions to document.write.






                                      share|improve this answer















                                      It is because the first parameter is a function call, whereas the second is only a function.



                                      For the second call to work as the first, you would do:



                                      document.write( (() => {return "<p>arrow-function</p>"})() );


                                      This syntax above is pretty messy: you need the extra () to call the function. On top of that, you need to surround all the arrow function definition within parentheses due to language syntax requirements.



                                      This way to immediately call functions from the very definition was really popular before ES2015 and is known as IIFE's. You usually see it with function statements but you can immediately call arrow functions too.



                                      On the other hand, for the first line of your snippet to work as your original second, you would do instead:



                                      document.write(output);


                                      So now you're passing just two function definitions to document.write.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 15 '18 at 15:50

























                                      answered Nov 13 '18 at 22:16









                                      SergeonSergeon

                                      2,515818




                                      2,515818






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290287%2fwhy-does-javascript-es6-function-calls-would-not-work-as-expected%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Florida Star v. B. J. F.

                                          Danny Elfman

                                          Lugert, Oklahoma