The Now Platform® Washington DC release is live. Watch now!
Sometimes the most innocent-looking code can really bend your mind — especially if it violates some deeply held notion you have about how things actually work inside a computer. That's exactly what's happened to the formerly normal fellow at right after running this unexpectedly diabolical snippet of code:
test();
function test() {
var a = 1;
gs.log('a outer: ' + a);
for (var b = 0; b < 2; b++) {
var a = 2;
gs.log('a inner: ' + a);
gs.log('b inner: ' + b);
}
gs.log('a outer: ' + a);
gs.log('b outer: ' + b);
}
But what he actually got was this:
a outer: 1
a inner: 2
b inner: 0
a inner: 2
b inner: 1
a outer: 1
b outer: undefined
What in Knuth's name is going on here? Those last two log lines are just wrong!
a outer: 1
a inner: 2
b inner: 0
a inner: 2
b inner: 1
a outer: 2
b outer: 2
var a = 1;
var a = 2;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.