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

Adam N
Adam N
59,192 Points

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,458 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.