Consider the following code in JavaScript-
var DEFAULT_RATE=0.01;
var rate=0.04;
function getRate() {
if(!rate) {
var rate=DEFAULT_RATE;
}
return rate;
}
console.log(`Rate is`,getRate());
The output of the following code is 0.01 My takeaway from this is that vars are hoisted(and declared undefined) to the top of the scope pertaining to the where they are used. In case of let and const, they go into TDZ but that is a different thing entirely.
But I also learnt that var has a global scope. Then var show not allow variable redeclaration.
Isn't the global scope mean the scope containing the entire code? Have I understood it wrongly?