Instagram | Twitter | LinkedIn
What is the output of the following code ?
A. undefined
B. 馃帺
C. 馃憭
馃憖 Click here to see the correct answer and explanation
A. undefined
var variables are "function scope". What does this mean? It means they are only available inside the function they鈥檙e created in. In this case, the variable declaration (var hat) whose scope is the function Accessory(), is hoisted to the top of the function.
This is how the interpreter views the above code:
var hat = "馃帺";
function Accessory() {
var hat;
console.log(hat);
hat = "馃憭";
}
Accessory();
Because of this, we can use variables before we declare them. However, we have to be careful because the hoisted variable is initialised with a value of undefined. The best option would be to declare and initialise our variable before use.
Explanation based on 馃憠馃徏 Hoisting & Understanding Hoisting in JavaScript
驴 Qu茅 imprime el siguiente c贸digo ?
A. undefined
B. 馃帺
C. 馃憭
馃憖 Haz click aqu铆 para ver la respuesta correcta y su explicaci贸n
A. undefined
Las variables var son "function scope". 驴Qu茅 significa esto? Significa que solo est谩n disponibles dentro de la funci贸n en la que se crean. En este ejemplo, la declaraci贸n de variable (var hat) cuyo alcance es la funci贸n Accessory(), se eleva a la parte superior de la funci贸n.
As铆 es como el int茅rprete ve el c贸digo anterior:
var hat = "馃帺";
function Accessory() {
var hat;
console.log(hat);
hat = "馃憭";
}
Accessory();
Debido a esto, podemos usar variables antes de declararlas. Sin embargo, debemos tener cuidado porque la variable elevada se inicializa con un valor de undefined. La mejor opci贸n ser铆a declarar e inicializar nuestra variable antes de usarla.
Explicaci贸n basada en 馃憠馃徏 Hoisting & The Difference Between Function and Block Scope in JavaScript


Hey, Teff. Est谩 genial!!
not茅 que escribiste initialised, creo que quisiste escribir initialized. No es nada importante, pero no quiero que te critiquen por ello...