Skip to content

Instantly share code, notes, and snippets.

@teffcode
Created May 29, 2020 16:26
Show Gist options
  • Select an option

  • Save teffcode/0b240584a41b1b3077762ce0d7c3bad9 to your computer and use it in GitHub Desktop.

Select an option

Save teffcode/0b240584a41b1b3077762ce0d7c3bad9 to your computer and use it in GitHub Desktop.

馃憢馃徏 Welcome 馃憢馃徏

Quiz banner

Instagram | Twitter | LinkedIn


馃憛 Choose your language



馃殌 English version


What is the output of the following code ?

  A. Reference Error
  B. "Call of Duty: WWII"
  C. "Star Wars: Battlefront II"

馃憖 Click here to see the correct answer and explanation
Correct Answer Explanation
B A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function鈥檚 variables. Therefore, in this example, since game1 is not defined in the inner function, the scope of the outer function is searched for a defined variable game1, which is found to have a value of "Call of Duty: WWII". Is important to mention, the scope of game1 is limited to the outer function, and the scope of game2 is limited to the inner function.

Explanation based on 馃憠馃徏 A simple guide to help you understand closures in JavaScript



馃殌 Spanish version


驴 Qu茅 imprime el siguiente c贸digo ?

  A. Reference Error
  B. "Call of Duty: WWII"
  C. "Star Wars: Battlefront II"

馃憖 Haz click aqu铆 para ver la respuesta correcta y su explicaci贸n
Respuesta correcta Explicaci贸n
B Un closure es una caracter铆stica en JavaScript en donde una funci贸n interna tiene acceso a las variables de la funci贸n externa (que la encierra). Por lo tanto, en este ejemplo, dado que la variable game1 no est谩 definida en la funci贸n interna, se busca en el alcance de la funci贸n externa y por esta raz贸n, la salida del c贸digo es: "Call of Duty: WWII". Es importante mencionar que el alcance de game1 est谩 limitado a la funci贸n externa, y el alcance de game2 est谩 limitado a la funci贸n interna.

Explicaci贸n basada en 馃憠馃徏 A simple guide to help you understand closures in JavaScript



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment