How is () => … different from () =>

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












I've found a weird issue.



given a filter and an array of objects, I would like to select only those objects that match the filter.



Weirdly, this doesn't work



this.state.articles.filter((article) => 
article.category === filter
)


while this does



this.state.articles.filter((article) => article.category === filter )


I originally thought they would evaluate the same, but it doesn't seem to be the case. Any ideas why?










share|improve this question





















  • The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
    – ibrahim mahrir
    35 mins ago






  • 2




    (article) => article.category === filter ) is (article) => return article.category === filter )
    – epascarello
    34 mins ago










  • See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Gabriele Petrioli
    33 mins ago






  • 1




    Possible duplicate of When should I use `return` in es6 Arrow Functions?
    – Ivar
    32 mins ago














up vote
7
down vote

favorite












I've found a weird issue.



given a filter and an array of objects, I would like to select only those objects that match the filter.



Weirdly, this doesn't work



this.state.articles.filter((article) => 
article.category === filter
)


while this does



this.state.articles.filter((article) => article.category === filter )


I originally thought they would evaluate the same, but it doesn't seem to be the case. Any ideas why?










share|improve this question





















  • The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
    – ibrahim mahrir
    35 mins ago






  • 2




    (article) => article.category === filter ) is (article) => return article.category === filter )
    – epascarello
    34 mins ago










  • See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Gabriele Petrioli
    33 mins ago






  • 1




    Possible duplicate of When should I use `return` in es6 Arrow Functions?
    – Ivar
    32 mins ago












up vote
7
down vote

favorite









up vote
7
down vote

favorite











I've found a weird issue.



given a filter and an array of objects, I would like to select only those objects that match the filter.



Weirdly, this doesn't work



this.state.articles.filter((article) => 
article.category === filter
)


while this does



this.state.articles.filter((article) => article.category === filter )


I originally thought they would evaluate the same, but it doesn't seem to be the case. Any ideas why?










share|improve this question













I've found a weird issue.



given a filter and an array of objects, I would like to select only those objects that match the filter.



Weirdly, this doesn't work



this.state.articles.filter((article) => 
article.category === filter
)


while this does



this.state.articles.filter((article) => article.category === filter )


I originally thought they would evaluate the same, but it doesn't seem to be the case. Any ideas why?







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 36 mins ago









mark

440315




440315











  • The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
    – ibrahim mahrir
    35 mins ago






  • 2




    (article) => article.category === filter ) is (article) => return article.category === filter )
    – epascarello
    34 mins ago










  • See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Gabriele Petrioli
    33 mins ago






  • 1




    Possible duplicate of When should I use `return` in es6 Arrow Functions?
    – Ivar
    32 mins ago
















  • The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
    – ibrahim mahrir
    35 mins ago






  • 2




    (article) => article.category === filter ) is (article) => return article.category === filter )
    – epascarello
    34 mins ago










  • See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Gabriele Petrioli
    33 mins ago






  • 1




    Possible duplicate of When should I use `return` in es6 Arrow Functions?
    – Ivar
    32 mins ago















The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
– ibrahim mahrir
35 mins ago




The first one uses a block of code, so a return statement is needed. The second one uses the implicit return of an arrow function
– ibrahim mahrir
35 mins ago




2




2




(article) => article.category === filter ) is (article) => return article.category === filter )
– epascarello
34 mins ago




(article) => article.category === filter ) is (article) => return article.category === filter )
– epascarello
34 mins ago












See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Gabriele Petrioli
33 mins ago




See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Gabriele Petrioli
33 mins ago




1




1




Possible duplicate of When should I use `return` in es6 Arrow Functions?
– Ivar
32 mins ago




Possible duplicate of When should I use `return` in es6 Arrow Functions?
– Ivar
32 mins ago












3 Answers
3






active

oldest

votes

















up vote
13
down vote













When you open a block in an arrow function, the return isn't implied anymore.



You have to write it down :



this.state.articles.filter((article) => 
return article.category === filter
)





share|improve this answer
















  • 2




    "implicit return" is the term if anyone wants to Google it.
    – Max Baldwin
    33 mins ago










  • This is equivalent to function(article) [...] , in ES5 and older, while (article) => article.category === filter is equivalent to function(article) return [...] .
    – Ismael Miguel
    33 mins ago


















up vote
4
down vote













Javascript ES6 arrow functions work in a particular manner which can best be described via an example:






let multiply1 = (number) => number * 2;
// When we are returning one value we can put this expression on the same line

// this is the same as:
let multiply2 = (number) => return number * 2;

//when we have 1 argument we can omit the parentheses
let multiply3 = number => number * 2;


// When we want to write multiple line we have to put brackets like this:
let multiply4 = (number) =>
console.log('inside arrow function');
return number * 2;
;

console.log(multiply1(2));
console.log(multiply2(2));
console.log(multiply3(2));
console.log(multiply4(2));





When the arrow function is returning an expression it is very convenient to not have to explicitly write the return statement and the square brackets . This allows for more concise code.






share|improve this answer



























    up vote
    0
    down vote













    The difference is that when you use () => x, it really means () => return x , so just how the statement article.category === filter on its own dosen't do anything, article.category === filter dosen't explicitly return anything.






    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',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      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%2f52334319%2fhow-is-different-from%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      13
      down vote













      When you open a block in an arrow function, the return isn't implied anymore.



      You have to write it down :



      this.state.articles.filter((article) => 
      return article.category === filter
      )





      share|improve this answer
















      • 2




        "implicit return" is the term if anyone wants to Google it.
        – Max Baldwin
        33 mins ago










      • This is equivalent to function(article) [...] , in ES5 and older, while (article) => article.category === filter is equivalent to function(article) return [...] .
        – Ismael Miguel
        33 mins ago















      up vote
      13
      down vote













      When you open a block in an arrow function, the return isn't implied anymore.



      You have to write it down :



      this.state.articles.filter((article) => 
      return article.category === filter
      )





      share|improve this answer
















      • 2




        "implicit return" is the term if anyone wants to Google it.
        – Max Baldwin
        33 mins ago










      • This is equivalent to function(article) [...] , in ES5 and older, while (article) => article.category === filter is equivalent to function(article) return [...] .
        – Ismael Miguel
        33 mins ago













      up vote
      13
      down vote










      up vote
      13
      down vote









      When you open a block in an arrow function, the return isn't implied anymore.



      You have to write it down :



      this.state.articles.filter((article) => 
      return article.category === filter
      )





      share|improve this answer












      When you open a block in an arrow function, the return isn't implied anymore.



      You have to write it down :



      this.state.articles.filter((article) => 
      return article.category === filter
      )






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 35 mins ago









      Zenoo

      8,97632043




      8,97632043







      • 2




        "implicit return" is the term if anyone wants to Google it.
        – Max Baldwin
        33 mins ago










      • This is equivalent to function(article) [...] , in ES5 and older, while (article) => article.category === filter is equivalent to function(article) return [...] .
        – Ismael Miguel
        33 mins ago













      • 2




        "implicit return" is the term if anyone wants to Google it.
        – Max Baldwin
        33 mins ago










      • This is equivalent to function(article) [...] , in ES5 and older, while (article) => article.category === filter is equivalent to function(article) return [...] .
        – Ismael Miguel
        33 mins ago








      2




      2




      "implicit return" is the term if anyone wants to Google it.
      – Max Baldwin
      33 mins ago




      "implicit return" is the term if anyone wants to Google it.
      – Max Baldwin
      33 mins ago












      This is equivalent to function(article) [...] , in ES5 and older, while (article) => article.category === filter is equivalent to function(article) return [...] .
      – Ismael Miguel
      33 mins ago





      This is equivalent to function(article) [...] , in ES5 and older, while (article) => article.category === filter is equivalent to function(article) return [...] .
      – Ismael Miguel
      33 mins ago













      up vote
      4
      down vote













      Javascript ES6 arrow functions work in a particular manner which can best be described via an example:






      let multiply1 = (number) => number * 2;
      // When we are returning one value we can put this expression on the same line

      // this is the same as:
      let multiply2 = (number) => return number * 2;

      //when we have 1 argument we can omit the parentheses
      let multiply3 = number => number * 2;


      // When we want to write multiple line we have to put brackets like this:
      let multiply4 = (number) =>
      console.log('inside arrow function');
      return number * 2;
      ;

      console.log(multiply1(2));
      console.log(multiply2(2));
      console.log(multiply3(2));
      console.log(multiply4(2));





      When the arrow function is returning an expression it is very convenient to not have to explicitly write the return statement and the square brackets . This allows for more concise code.






      share|improve this answer
























        up vote
        4
        down vote













        Javascript ES6 arrow functions work in a particular manner which can best be described via an example:






        let multiply1 = (number) => number * 2;
        // When we are returning one value we can put this expression on the same line

        // this is the same as:
        let multiply2 = (number) => return number * 2;

        //when we have 1 argument we can omit the parentheses
        let multiply3 = number => number * 2;


        // When we want to write multiple line we have to put brackets like this:
        let multiply4 = (number) =>
        console.log('inside arrow function');
        return number * 2;
        ;

        console.log(multiply1(2));
        console.log(multiply2(2));
        console.log(multiply3(2));
        console.log(multiply4(2));





        When the arrow function is returning an expression it is very convenient to not have to explicitly write the return statement and the square brackets . This allows for more concise code.






        share|improve this answer






















          up vote
          4
          down vote










          up vote
          4
          down vote









          Javascript ES6 arrow functions work in a particular manner which can best be described via an example:






          let multiply1 = (number) => number * 2;
          // When we are returning one value we can put this expression on the same line

          // this is the same as:
          let multiply2 = (number) => return number * 2;

          //when we have 1 argument we can omit the parentheses
          let multiply3 = number => number * 2;


          // When we want to write multiple line we have to put brackets like this:
          let multiply4 = (number) =>
          console.log('inside arrow function');
          return number * 2;
          ;

          console.log(multiply1(2));
          console.log(multiply2(2));
          console.log(multiply3(2));
          console.log(multiply4(2));





          When the arrow function is returning an expression it is very convenient to not have to explicitly write the return statement and the square brackets . This allows for more concise code.






          share|improve this answer












          Javascript ES6 arrow functions work in a particular manner which can best be described via an example:






          let multiply1 = (number) => number * 2;
          // When we are returning one value we can put this expression on the same line

          // this is the same as:
          let multiply2 = (number) => return number * 2;

          //when we have 1 argument we can omit the parentheses
          let multiply3 = number => number * 2;


          // When we want to write multiple line we have to put brackets like this:
          let multiply4 = (number) =>
          console.log('inside arrow function');
          return number * 2;
          ;

          console.log(multiply1(2));
          console.log(multiply2(2));
          console.log(multiply3(2));
          console.log(multiply4(2));





          When the arrow function is returning an expression it is very convenient to not have to explicitly write the return statement and the square brackets . This allows for more concise code.






          let multiply1 = (number) => number * 2;
          // When we are returning one value we can put this expression on the same line

          // this is the same as:
          let multiply2 = (number) => return number * 2;

          //when we have 1 argument we can omit the parentheses
          let multiply3 = number => number * 2;


          // When we want to write multiple line we have to put brackets like this:
          let multiply4 = (number) =>
          console.log('inside arrow function');
          return number * 2;
          ;

          console.log(multiply1(2));
          console.log(multiply2(2));
          console.log(multiply3(2));
          console.log(multiply4(2));





          let multiply1 = (number) => number * 2;
          // When we are returning one value we can put this expression on the same line

          // this is the same as:
          let multiply2 = (number) => return number * 2;

          //when we have 1 argument we can omit the parentheses
          let multiply3 = number => number * 2;


          // When we want to write multiple line we have to put brackets like this:
          let multiply4 = (number) =>
          console.log('inside arrow function');
          return number * 2;
          ;

          console.log(multiply1(2));
          console.log(multiply2(2));
          console.log(multiply3(2));
          console.log(multiply4(2));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 21 mins ago









          Willem van der Veen

          2,5292721




          2,5292721




















              up vote
              0
              down vote













              The difference is that when you use () => x, it really means () => return x , so just how the statement article.category === filter on its own dosen't do anything, article.category === filter dosen't explicitly return anything.






              share|improve this answer
























                up vote
                0
                down vote













                The difference is that when you use () => x, it really means () => return x , so just how the statement article.category === filter on its own dosen't do anything, article.category === filter dosen't explicitly return anything.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  The difference is that when you use () => x, it really means () => return x , so just how the statement article.category === filter on its own dosen't do anything, article.category === filter dosen't explicitly return anything.






                  share|improve this answer












                  The difference is that when you use () => x, it really means () => return x , so just how the statement article.category === filter on its own dosen't do anything, article.category === filter dosen't explicitly return anything.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 13 mins ago









                  DeltaMarine101

                  186




                  186



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52334319%2fhow-is-different-from%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      Installing NextGIS Connect into QGIS 3?

                      One-line joke