Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Getting Started With ES2015 Create Functions Using Arrow Syntax Concise Arrow Function Syntax

Video seems to imply that this should work

At ~0:36

const hi = x => { x }

console.log(hi(5)) //undefined

1 Answer

andren
andren
28,519 Points

Yeah that bit in the video is a bit misleading. It seems to suggest that as long as there is only one line of code you can omit the return keyword, and also omit the braces if you want to. But in reality you actually need to omit the braces around the code, that is what marks the code as a concise arrow function.

In other words:

// Valid concise arrow function
const hi = x => x

// Invalid concise arrow function
const hi = x => { x }

So if you simply remove the braces in your code like this:

const hi = x => x
console.log(hi(5))

Then it will run fine.