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

Is creating a method using Arrow Syntax a safe practice?

Hello, Everyone!

In the Code Challenge: Adding a Method to an Object of the Object Oriented JavaScript course, I have tried to answer the question by creating a method using the Arrow Syntax. However, a message popped out saying that there was an error: SyntaxError: Parse error.

var contact = {
  fullName: () => {
    var firstName = "Andrew";
    var lastName = "Chalkley";
    console.log(firstName + " " + lastName);
  }
};

I checked the same code snippet from above using the Chrome DevTools and there was no error.

Is this a bug causing the code challenge to alert an error?

Cheers!

1 Answer

Steven Parker
Steven Parker
231,269 Points

In actual practice this should be fine as long as you remain aware of the ways that arrow functions are different. While they don't seem to pertain to this example, it could be that something done behind the scenes by the challenge checker may be affected by the differences. You might want to report this as a bug to Support.

However, it's worth noting that the MDN documentation page has this recommendation: "These function expressions are best suited for non-method functions."