var x = 3;
if(1==1){
if(2==2){
var x= 5;
}
}
document.write(x); // outputs 5
So I was running some tests to understand the concept of local and global scope better. I thought declaring x with keyword var creates a local variable and ignores any variable with the same name in the parent scope. By ignore, I mean how I can change the variable x without affecting the same variable in the scope above it. But apparently my understanding of the definition of js variables is wrong. could please explain why it behaves so?
xhave the same scopeif, and loops likefororwhiledon't create their own scope in JS