DEV Community

Cristian Fernando
Cristian Fernando

Posted on

3

Paracetamol.js💊| #222: Explica este código JavaScript

Explica este código JavaScript

Dificultad: Intermedio

const sum = (num1) => {
    return (num2) => {
        return num1 + num2
    }
}
console.log(sum(1)(2)) // 🤔🤔
Enter fullscreen mode Exit fullscreen mode
  • A. SyntaxError
  • B. 2
  • C. 3
  • D. 1

Respuesta en el primer comentario.

Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando

Respuesta

  • C. 3

Para comprender este ejemplo debemos saber que es un closure en JavaScript.
Como vemos la función sum que recibe el parámetro num1 regresa una función anónima con parámetro num2; dicha función anónima debe acceder al parámetro num1 que esta en un scope superior al suyo. Esto es perfectamente posible en JavaScript y es por ello que la respuesta es 3.

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay