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

171
Vistas
Javascript Closure - Why isn't undefined displayed?

I'm trying to understand how closure works in Javascript. In the last line, if I enter sayHi("Hello")("John Doe"), the console would display "HelloJohn Doe" as expected.

Now, if I remove ("John Doe"), how come the console turns blank instead of displaying "Helloundefined"?

    function sayHi(whattosay){
        return function yolo(name) {
            console.log(whattosay + name)
        }
    }
    sayHi("Hello")
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

That's because you still need to call the function, even if you don't want to provide the "name" parameter.

Try with : sayHi("whatever")()

and you will see the expected output!

whateverundefined

If you omit the last pair of parenthesis, you are just getting back a function that WOULD print the expected output if you called it, but you just aren't calling it yet.

so you could for example store that returned function in some variable, and call it later on.

about 4 years ago · Juan Pablo Isaza Denunciar

0

this will return your inner function yolo

function sayHi(whattosay){
    return function yolo(name) {
        console.log(whattosay + name)
    }
}
sayHi("Hello")

you need to call yolo function to see your console.log output

function sayHi(whattosay){
    return function yolo(name) {
        console.log(whattosay + name)
    }
}
sayHi("Hello")()

you can even store return function in a variable and call it

function sayHi(whattosay){
    return function yolo(name) {
        console.log(whattosay + name)
    }
}
const yolo = sayHi("Hello")

yolo()
about 4 years ago · Juan Pablo Isaza Denunciar
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