12

So node developers. Can anyone explain to me what the hell the "=>" operator is? Because I cannot find anything about it online.

Comments
  • 9
    How was I supposed to know it was called "fat arrow"?

    You would think googling for "=>" would tell me what it was.
  • 3
    () => {
    //code here
    }

    Is essentially

    function() {
    //code here
    }
  • 0
    The reason why it's so nice is because let's say you have some function that takes parameter foo, you can write

    foo => foo + 1

    Which returns foo + 1, the same as

    function(foo) {
    return foo + 1
    }

    It helps reduces boilerplating
  • 0
    @port22 that code would be using - >, using fat arrow passes this as a function parameter, or maybe that's Coffeescript specific?
  • 0
    @smithalan2 it's es6 not coffeescript
  • 1
    @port22 Thanks port22 :). See that's all I wanted. A polite answer :). I know a lot, but not everything :D
  • 0
    @port22 my bad, I could've sworn I saw Coffeescript somewhere here. More coffee is needed I think.
  • 2
    It's a little different than a regular function because it doesn't change the binding of 'this' to the function itself.

    I hope that made sense...JS can be confusing.
  • 1
    Fat arrow functions? I always heard them called lambda expressions or anonymous functions.
  • 0
    @port22 Can you give a contextual example? There's no assignment operator in yours and I'm not seeing the context of where the value is 'returning'...

    foo = 1;

    foo => foo + 1;

    console.log( foo ); // 1?
  • 1
    @ardinent cos you define a function but never use it.

    let foo = 2;

    inc = a => a+2;
    foo += inc();
    console.log(foo);

    Also when in doubt for language things, if you don't know what something is card, download the PDF language spec, search for the operator and find out what it's called that way.
  • 0
    @nmunro This seemed to be a thread discussing the usage of the operator, so I thought enquiring about it might share it's usage with more followers.

    Another question... Should you not be passing a value in your inc() call?
  • 0
    @ardinent Yes, yes I should, I also intended to use 1 not 2, guess I shouldn't code before coffee...
  • 0
    @nmunro hehe, no worries. Believe it or no, but it now answers my question.

    Thanks mate 😬
  • 0
    @ardinent however...

    It only implicitly returns something if the function is one line...

    That is to say

    Foo = a => {
    a++;
    return a + 5;
    }

    Must have the return statement in multiline functions
Add Comment