Implicit return in arrow functions
JavaScriptAll snippets in this category →
Posted on If an arrow function immediately returns a value, the return
keyword is optional. We also have to remove the curly brackets for this to work.
The return
is then implied, making it an “implicit return”.
// we could write the `return` explicitly
const getPumpedAbout = thing => {
return `Pumped about ${thing}!`
}
// if we write it like this, the `return` is implied
const getPumpedAbout = thing => `Pumped about ${thing}!`
// there is no difference in how we would call these functions
getPumpedAbout("implicit returns") // ⇒ "Pumped about implicit returns!"
// we could write the `return` explicitly
const getPumpedAbout = thing => {
return `Pumped about ${thing}!`
}
// if we write it like this, the `return` is implied
const getPumpedAbout = thing => `Pumped about ${thing}!`
// there is no difference in how we would call these functions
getPumpedAbout("implicit returns") // ⇒ "Pumped about implicit returns!"
// we could write the `return` explicitly
const getPumpedAbout = thing => {
return `Pumped about ${thing}!`
}
// if we write it like this, the `return` is implied
const getPumpedAbout = thing => `Pumped about ${thing}!`
// there is no difference in how we would call these functions
getPumpedAbout("implicit returns") // ⇒ "Pumped about implicit returns!"
// we could write the `return` explicitly
const getPumpedAbout = thing => {
return `Pumped about ${thing}!`
}
// if we write it like this, the `return` is implied
const getPumpedAbout = thing => `Pumped about ${thing}!`
// there is no difference in how we would call these functions
getPumpedAbout("implicit returns") // ⇒ "Pumped about implicit returns!"