Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

What does => mean in coffeescript?

What does => signify in coffeescript?

a = () -> console.log("hi")

compared to

a = () => console.log("hi")

1 Answer

Steven Parker
Steven Parker
243,318 Points

It appears to be a very subtle difference.

I'm not a Coffescript user, but I pasted your lines into a transpiler and looked at the resulting JavaScript.

The first line just assigns a to an anonymous function that logs "hi". The second line assigns a to the result of calling an anonymous function which returns another anonymous function that logs "hi":

//  a = () -> console.log("hi")
    a = function () {
        return console.log('hi');
    };

//  a = () => console.log("hi")
    a = function (_this) {
        return function () {
            return console.log('hi');
        };
    }(this);

So either way, a ends up being a function that logs "hi". But apparently the second way provides an opportunity (that is not used here) for the function to reference the original context using "_this".