Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

108
Vistas
Is a closure only created when a inner function returns something?

I have been playing around with closures a little bit and I have a difficult time understanding what's the difference between lexical scoping and closures.

In the examples below both child functions are able to reach the a variable thanks to lexical scoping. Now I wonder where exactly is the closure created in the second example? Is it because of the childFunc() being returned directly, or is it because of childFunc() being stored in a seperate child variable before being exectuted?

Example 1: No closure

function parentFunc() {
    let a = 10;

    function childFunc() {
        console.log(`hey from child func Mr. ${a}`);
        a++;
    }
    childFunc();
}
console.dir(parentFunc) //closure not part of the Scopes

parentFunc() //doesn't increment a
parentFunc()

Example 2: Closure

function parentFunc() {
    let a = 10;

    return function childFunc() {
        console.log(`hey from child func Mr. ${a}`);
        a++;
    }
}
const child = parentFunc()
child(); // increments a
child();

console.dir(parentFunc) //closure is part of the scopes
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda