When arrow functions are used, if they consist of just one line, the curly braces { } can be omitted and the return value of the expression will automatically become the return value of the function.
In simple terms:
Spark.get("/facture", (req, res) =>
chalk.green('Hello word');
)
This translates to:
Spark.get("/facture", function (req, res) {
return chalk.green('Hello word');
});
However, when there are more than one statement in an arrow function and a body is created, you need to manually add a return statement just like in regular functions.
The difference becomes clear when looking at the transpiled code.
Spark.get("/facture", (req, res) => {
chalk.red('Hello test');
chalk.green('Hello word');
})
This transpiles to:
Spark.get("/facture", function (req, res) {
chalk.red('Hello test');
chalk.green('Hello word');
});
To explicitly return a value, the return statement must be included:
Spark.get("/facture", (req, res) => {
chalk.red('Hello test');
return chalk.green('Hello word');
})
This is how it looks in plain javascript:
Spark.get("/facture", function (req, res) {
chalk.red('Hello test');
return chalk.green('Hello word');
});
For further examples, you can visit the playground here and read more about arrow functions on the MDN page dedicated to them here.