Instagram | Twitter | LinkedIn
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
驴 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

