DEV Community

Cover image for Explicit and implicit return in function (Spanish)
Alfredo Carreón Urbano
Alfredo Carreón Urbano

Posted on

7 2

Explicit and implicit return in function (Spanish)

Antes de iniciar.

Explícito: Que expresa algo con claridad.
Implícito: Que está incluido, sin que está lo especifique.

Retorno explícito.

Las funciones regulares y las funciones flecha pueden comportarse de forma similar, ya que podemos devolver un valor explícitamente, utilizando la palabra reservada “return”.

Función regular.

function add(x,y) { // Statements
  return x + y;
}
Enter fullscreen mode Exit fullscreen mode

Función flecha.

const add = (x,y) => {
  return x + y;
}
Enter fullscreen mode Exit fullscreen mode

Retorno implícito.

Las funciones flecha tiene una ventaja sobre las funciones regulares, ya que las funciones flecha pueden devolver un valor de forma implícitamente, simplemente omitimos las llaves que normalmente envuelven el cuerpo de una función.

Nota: El implícito es exclusivo de las funciones flecha.

const increment = x => ++x; // Expression
Enter fullscreen mode Exit fullscreen mode

Cuando se usan retornos implícitos, los objetos deben estar entre paréntesis para que las llaves no se confundan con la apertura del cuerpo de la función.

const obj = () => { name: "Victor" }; // return undefined
const obj = () => ({ name: "Victor" }); // return { name: "Victor"}
Enter fullscreen mode Exit fullscreen mode

¿Cuándo usarlos?

Si tu función necesita varias declaraciones entonces retorna de forma explícita, de lo contrario use la forma implícita y ahorra lineas de código.

Espero les guste :)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay