These are 2 examples which must have attained same result:
Example 1
<script>
console.log(a);
var a = 10;
console.log(a);
</script>
Rendered
<script>
var a = "";
console.log(a); //will result undefined
a = 10;
console.log(a); //will result 10
</script>
Result
undefined
10
Example 2
<script>
console.log(a);
a = 10;
console.log(a);
</script>
Expectation of Rendering
<script>
var a = "";
console.log(a); //should result undefined
a = 10;
console.log(a); //should result 10
</script>
Result
Now, as per JS Hoisting in Scenario 2, the variable if not declared must have been automatically declared onto top of its scope and still result should have been the same. Why is it not? Where is the concept failed?
These are 2 examples which must have attained same result:
Example 1
<script>
console.log(a);
var a = 10;
console.log(a);
</script>
Rendered
<script>
var a = "";
console.log(a); //will result undefined
a = 10;
console.log(a); //will result 10
</script>
Result
undefined
10
Example 2
<script>
console.log(a);
a = 10;
console.log(a);
</script>
Expectation of Rendering
<script>
var a = "";
console.log(a); //should result undefined
a = 10;
console.log(a); //should result 10
</script>
Result
Now, as per JS Hoisting in Scenario 2, the variable if not declared must have been automatically declared onto top of its scope and still result should have been the same. Why is it not? Where is the concept failed?
Share Improve this question asked Aug 27, 2016 at 14:14 DeadpoolDeadpool 8,2509 gold badges48 silver badges95 bronze badges 5- Where do you get the rendered code information from? – trincot Commented Aug 27, 2016 at 14:16
-
1
Just put
var a = 10
and you'll get what you expect.a = 10
is not the same ofvar a = 10
: stackoverflow./questions/1470488/… – Gerardo Furtado Commented Aug 27, 2016 at 14:18 - I wrote how JS must be rendering the code! – Deadpool Commented Aug 27, 2016 at 14:18
-
1
""
is not undefined. – melpomene Commented Aug 27, 2016 at 14:19 - OK, @Peterson, because it is wrong :) – trincot Commented Aug 27, 2016 at 14:19
4 Answers
Reset to default 7The second case is different, because
a = 10
... does not declare a hoisted variable. It creates a property in the window
object (even if the code would have been inside a function). This is not a declaration, and thus something that is not hoisted. So before you create that property, it does not exist.
Note that what you listed as rendered code is not entirely correct. A hoisted variable does not get a value, so for your first example it should look like this:
var a; // undefined!
console.log(a); // will output undefined
a = 10;
console.log(a); // will output 10
Note that if this code is not part of a function body, var a
also creates the window.a
property, and this happens at the hoisted declaration.
And for your second example, the rendered code could look like this
console.log(a); // Error: does not exist.
window.a = 10;
console.log(a); // will output 10
JavaScript hoists declarations, not initializations, see this page.
If you add a 'var a;' somewhere in your second example, it should work fine!
On example 2, at the time you run the first console log, a
is really undefined.
a = 10 sets a new property a
to the window
object and there's no 'hoisting' when setting a property to an object that already exists.
when we will execute a source code .IN hoisting, The declaration variable goes over console.log().Then the variable allowcated a memory but does not access it. When I declaration a variable in memory. Then we give by default undefined value in place of that variable. (as like ,in html,place Holder works like this. --- So you have not declared any variable. Hosting will not work here. --- Can't access a here..Because when you define a variable. Then you must be defined a variable.here u can not make variable so output: cannot access a