The following javascript code
var variable = 1
console.log(variable)
{
var variable = 2
}
console.log(variable)
gives the output
1
2
Given the way scopes work in other programming languages I find this very unintuitive. The redeclaration of variable inside the scope shouldn't affect the value of the variable outside the scope.
I tested this on Firefox and Chromium. Am I doing something wrong or is this the expected behavior of javascript?
As a reference, this is what happens in other programming languages:
#include<stdio.h>
int main()
{
int variable=1;
printf("%i\n",variable);
{
int variable=2;
}
printf("%i\n",variable);
return 0;
}
output:
1
1