DEV Community

Discussion on: How JavaScript engine execute this code?

Collapse
 
coderoo7 profile image
Shubham

Well thanks for your reply. I understand hoisting concept but what I'm not able to understand what happen when JavaScript engine compile the code.

console.log(a); log 'undefined'
var a=10;

According to you var a hoist to top but what I actually understand is that when JS engine compile the code it set the var 'a' to top and set it to undefined and at time of execution assignment operation is done.
That's why log is undefined for var 'a' but when JS engine read a=10 then it assign the value 10 to var a.

Collapse
 
uddeshjain profile image
Uddesh • Edited

Yes exactly, each and every variable assigned undefined unless you assign some value to it that's why console.log(a) says undefined.