I am running Edge/15.15063. 'Can I Use' says const should work.
Running:
const x = 'woo'
Then:
console.log(x)
Returns
'x' is undefined
Screenshot:
Why isn't const working?
I suspect that the Edge console is using a with
statement under its covers like other implementations did. This would explain var
s and even function
declarations being hoisted outside into the global scope, but let
and const
will be locked into the block scope:
with (…) {
const x = 'woo'
}
// next input:
with (…) {
console.log(x) // obviously undeclared
}
Try entering them in multiline mode, in a single evaluation - there they should work.
But you also might want to file a bug, as the console is indeed expected to feel like evaluating things in the global scope.
I think I figured this out, but this is as much a guess as an answer. Too long for a comment though.
I think what's happening is that const
and let
do not create implicit globals when used in the top-level scope in the same way var
does. Although top-level variables created with const
and let
are global, they are not properties of the global window
object.
If the MS console is relying on that implicit window
property creation for accessing variables created in the console, then const
and let
will not work.
I am unsure of the inner workings of Chrome Dev Tools, but it seems to create an anonymous function wrapper for code executed in the console:
throw new Error;
VM679:1 Uncaught Error at anonymous:1:7
(function() { throw new Error; })();
VM759:1 Uncaught Error at anonymous:1:21 at anonymous:1:33
I am unsure if there is other sandboxing going on here, I didn't necessarily find a lot of documentation on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With