Is it possible to export Arrow functions in ES6/7?

JavascriptEcmascript 6Arrow Functions

Javascript Problem Overview


The export statement below gives a syntax error

export default const hello = () => console.log("say hello")

why ?

I'm only able to export named functions

export function hello() {
  console.log("hello")
}

What is the reason?

Javascript Solutions


Solution 1 - Javascript

> Is it possible to export Arrow functions in ES6/7?

Yes. export doesn't care about the value you want to export.

> The export statement below gives a syntax error ... why?

You cannot have a default export and give it a name ("default" is already the name of the export).

Either do

export default () => console.log("say hello");

or

const hello = () => console.log("say hello");
export default hello;

Solution 2 - Javascript

If you don't want a default export, you can simply export a named function with this syntax:

export const yourFunctionName = () => console.log("say hello");

Solution 3 - Javascript

Try this

export default () => console.log("say hello");

or

export const hello = () => console.log("say hello")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjozzyView Question on Stackoverflow
Solution 1 - JavascriptFelix KlingView Answer on Stackoverflow
Solution 2 - JavascriptRaphael PinelView Answer on Stackoverflow
Solution 3 - JavascriptRaghurajView Answer on Stackoverflow